Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to convert month to number in python

import calendar

months = {month: index for index, month in enumerate(calendar.month_abbr) if month}

# {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
Comment

python month number from date

import datetime
date = '2021-05-21 11:22:03'
datem = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
print(datem.day)        # 25
print(datem.month)      # 5
print(datem.year)       # 2021
print(datem.hour)       # 11
print(datem.minute)     # 22
print(datem.second)     # 3
Comment

converting month number to month name python

months_in_year = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
date = '2021-11-21 11:22:03'
month = date.dt.month
print(month)
#Output
11

month_name = months_in_year [month - 1]
print(month_name)
#Output
November
Comment

PREVIOUS NEXT
Code Example
Python :: python hour from date 
Python :: python day number from date 
Python :: how to create a car game using python 
Python :: how to find the lowest value in a nested list python 
Python :: python print range 
Python :: load saved model pyspark 
Python :: np not defined 
Python :: sns seaborn set theme 
Python :: determinant of a matrix in python 
Python :: reduced fraction python 
Python :: ionic python2 Error: not found: python2 
Python :: how to clear the console python 
Python :: pandas dataframe show one row 
Python :: send image discord.py 
Python :: set font size xaxis pandas 
Python :: Import "decouple" could not be resolved Pylance 
Python :: pymysql check if table exists 
Python :: python format only 1 decimal place 
Python :: plt.xlabel not working 
Python :: RandomForestRegressor import 
Python :: python timeit commandline example 
Python :: python datetime minus 1 day 
Python :: python save figure as pdf 
Python :: get output of ps aux grep python 
Python :: python parser txt to excel 
Python :: python extract name out of mail 
Python :: python write yaml 
Python :: bail bond cowboys 
Python :: plot_histogram qiskit pycharm 
Python :: concat tensors pytorch 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =