How do I convert 20230102
to Monday
?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd
.
How do I convert 20230102
to Monday
?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd
.
Parse with strptime and format with strftime:
>>> from datetime import datetime
>>> n = 20230102
>>> datetime.strptime(str(n), "%Y%m%d").strftime("%A")
'Monday'
See strftime()
and strptime()
Format Codes for documentation of the % strings.
You can do it with weekday()
method.
from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()] #Friday
You can convert number string into weekday using "datetime" module
import datetime
def get_weekday(date):
date = datetime.datetime.strptime(date, '%Y%m%d')
return date.strftime('%A')
print(get_weekday('20230102'))
This is how you can achieve your desired output.