Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize string to datetime format in Python

I want to transfer date string into python datetime format. The string is below:

Mon, 26 Dec 2011 20:42:08 +0200
Sat, 24 Dec 2011 16:28:59 +0200

Is there any faster way to transfer the string into python datetime format without using pytz API?

like image 982
Jimmy Lin Avatar asked Mar 06 '26 21:03

Jimmy Lin


1 Answers

>>> import datetime
>>> s = "Mon, 26 Dec 2011 20:42:08 +0200"
>>> t = datetime.datetime.strptime(s, "%a, %d %b %Y %H:%M:%S %z")
>>> t
datetime.datetime(2011, 12, 26, 20, 42, 8, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))

See the docs for a complete list of datetime placeholders.

like image 141
Tim Pietzcker Avatar answered Mar 08 '26 10:03

Tim Pietzcker