0
today = datetime.strptime('Sep 3 2021 2:58 pm', '%b %d %Y %I:%M %p')

Returns the following error: time data Sep 03 2021 2:58 pm does not match format %b %d %Y %I:%M %p

Equinox
  • 6,483
  • 3
  • 23
  • 32
Marco K
  • 35
  • 4
  • 1
    works fine for me, which Python version are you using? Also, did you maybe set a specific [locale](https://docs.python.org/3/library/locale.html)? – FObersteiner Jan 19 '22 at 15:23

1 Answers1

0

When I run it, it works fine. Perhaps you can try the following:

today = datetime.strptime('Sep 3 2021 2:58 pm', '%b %-d %Y %I:%M %p')

%-d should be used if the day number is not zero-padded (i.e. Sep 3 instead of Sep 03). According to this website this is platform-specific, so perhaps you are using a platform that requires you to specify it is not zero-padded.

MK95
  • 21
  • 5
  • I'm using windows. I've tried adding "-", however it just returns that it is a bad directive for the format. I'm on Python 3.9.7 according to anaconda – Marco K Jan 19 '22 at 15:28
  • Apparently, padding is only an issue with strftime, not strptime according to this question: https://stackoverflow.com/questions/25279993/parsing-non-zero-padded-timestamps-in-python/25280059, not sure why it doesn't work for you, sorry! – MK95 Jan 19 '22 at 15:33
  • Well, I restarted the kernel and it worked. No idea why. Thank you very much sir. Wouldn't have tried this if t wasn't for what you said! – Marco K Jan 19 '22 at 15:36
  • Great to hear, good luck! – MK95 Jan 19 '22 at 15:38