Python Forum

Full Version: Date format error getting weekday value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I get the following error on line 4 when running the code below:
valueError: timedata '05/09/2022' does not match format '%m %d %Y'

I am trying to get a weekday value from 0-6 from a date represented in a string.

1 #!/usr/bin/env python3
2 import datetime
3 strdate = "05/29/2022"
4 day=datetime.datetime.strptime(strdate, "%m %d %Y").weekday()
5 print("weekday = ",day)
You have

'05/09/2022'

the you should use

"%m/%d/%Y"
(May-29-2022, 06:56 PM)Axel_Erfurt Wrote: [ -> ]You have

'05/09/2022'

the you should use

"%m/%d/%Y"

Thank you very very much, your reply fixed the problem!!!