0

this is working

datetime.strptime('2012-03-01T10:00:00Z', '%Y-%m-%dT%H:%M:%SZ') 

same need to do for

datetime.strptime('Mon Mar 14 05:21:08 UTC 2022', '<format>')

what would be format for same

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
Abhishek
  • 89
  • 1
  • 4
  • You can find the list of codes [in the documentation](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes) – mozway Mar 23 '22 at 12:58

2 Answers2

3

You can use the parser in dateutil.

from dateutil import parser
my_date = parser.parse('Mon Mar 14 05:21:08 UTC 2022')
print(my_date) # datetime.datetime(2022, 3, 14, 5, 21, 8, tzinfo=tzutc())
starklikos
  • 154
  • 1
  • 1
  • 8
1

Just to explicitly provide the corresponding format code for that timestamp

>>> datetime.strptime('Mon Mar 14 05:21:08 UTC 2022', '%a %b %d %H:%M:%S %Z %Y')
datetime.datetime(2022, 3, 14, 5, 21, 8)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Problem with this is that %Z *accepts* "UTC" but doesn't do anything with it - so resulting datetime object is naive and thus represents local time – FObersteiner Mar 23 '22 at 13:31