Python Forum
Date format error getting weekday value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date format error getting weekday value
#1
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)
Reply
#2
You have

'05/09/2022'

the you should use

"%m/%d/%Y"
Aggie64 likes this post
Reply
#3
(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!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid Date Format fo Cached Files jland47 1 262 May-22-2024, 07:04 PM
Last Post: deanhystad
  Compare current date on calendar with date format file name Fioravanti 1 444 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 875 Jan-20-2024, 04:45 AM
Last Post: 1418
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 850 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Modifying a date format jehoshua 17 3,501 Oct-29-2022, 08:44 PM
Last Post: jehoshua
  Invalid format specifier Error Led_Zeppelin 2 8,366 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  Convert Date to another format lonesoac0 2 1,784 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Format SAS DATE Racer_x 0 1,075 Feb-09-2022, 04:44 PM
Last Post: Racer_x
  How can I compare 2 format of date? korenron 4 1,669 Dec-21-2021, 12:40 PM
Last Post: korenron
  Date format and past date check function Turtle 5 4,673 Oct-22-2021, 09:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020