-1

How do I convert 20230102 to Monday?

Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd.

petezurich
  • 9,280
  • 9
  • 43
  • 57

3 Answers3

2

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.

wim
  • 338,267
  • 99
  • 616
  • 750
0

You can do it with weekday() method.

from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()]  #Friday
petezurich
  • 9,280
  • 9
  • 43
  • 57
0

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.

Finix
  • 1
  • 3