93

I have a date time string that I don't know how to parse it in Python.

The string is like this:

Tue May 08 15:14:45 +0800 2012

I tried

datetime.strptime("Tue May 08 15:14:45 +0800 2012","%a %b %d %H:%M:%S %z %Y")

but Python raises

'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'

According to Python doc:

%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).

What is the right format to parse this time string?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
xiaohan2012
  • 9,870
  • 23
  • 67
  • 101
  • related: [Python: parsing date with timezone from an email](http://stackoverflow.com/q/1790795/4279) – jfs Aug 09 '15 at 15:01

5 Answers5

129

datetime.datetime.strptime has problems with timezone parsing. Have a look at the dateutil package:

>>> from dateutil import parser
>>> parser.parse("Tue May 08 15:14:45 +0800 2012")
datetime.datetime(2012, 5, 8, 15, 14, 45, tzinfo=tzoffset(None, 28800))
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • any ideas on how to parse `02/Nov/2012:06:37:42 +0000`? That's the format nginx uses in log files, and `parser.parse` returns `ValueError: unknown string format`. – zidarsk8 Nov 02 '12 at 22:16
  • @zidarsk8: You first need to remove the colon after the date: ```parser.parse("06/Apr/2014:13:23:04".replace(":", " ", 1))``` – Emil Stenström Apr 07 '14 at 09:43
  • 9
    @zidarsk8 try this: `parser.parse("02/Nov/2012:06:37:42 +0000",fuzzy=True)` - specifying fuzzy, the parser ignores characters it doesn't understand. – drevicko Nov 07 '14 at 00:28
  • 3
    This actually parses the date incorrectly. `Fri Nov 9 09:04:02 2012 -0500` ignores `-0500` and uses the current time zone. – Whitecat Aug 15 '16 at 20:58
  • 3
    The command is "pip install python-dateutil" to install it using pip btw. – Thomas4019 Feb 01 '17 at 17:11
  • 2
    Be careful - using this library I also got incorrect results, the month and the day flipped! 19.01.2017 => January, 12.01.2017=>December – Nir Jun 01 '18 at 13:37
  • Depending on what you want to parse, remember to use `dayfirst=True`, otherwise incorrect results can be returned. (See also [this answer](https://stackoverflow.com/a/27800808/1611927)) – Michele Nov 01 '18 at 18:52
  • If you have trouble importing `parser`, do: `from dateutil.parser import parse`. – Rob Truxal Mar 18 '19 at 23:50
  • In Python 3.7 is finally possible to parse the timezone in the format `+HH:MM` – Jacopofar May 15 '19 at 12:42
  • 1
    @eumiro I just proposed an edit to your answer, but my edit is wrong, and I can't figure out how to cancel the edit. Please reject it. – falsePockets Sep 06 '19 at 01:58
  • @Nir This is why I don't trust packages very easily. – codingbruh May 22 '23 at 04:20
40

Your best bet is to have a look at strptime()

Something along the lines of

>>> from datetime import datetime
>>> date_str = 'Tue May 08 15:14:45 +0800 2012'
>>> date = datetime.strptime(date_str, '%a %B %d %H:%M:%S +0800 %Y')
>>> date
datetime.datetime(2012, 5, 8, 15, 14, 45)

Im not sure how to do the +0800 timezone unfortunately, maybe someone else can help out with that.

The formatting strings can be found at http://docs.python.org/library/time.html#time.strftime and are the same for formatting the string for printing.

Hope that helps

Mark

PS, Your best bet for timezones in installing pytz from pypi. ( http://pytz.sourceforge.net/ ) in fact I think pytz has a great datetime parsing method if i remember correctly. The standard lib is a little thin on the ground with timezone functionality.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Mark Lakewood
  • 1,980
  • 4
  • 22
  • 44
7

Here's a stdlib solution that supports a variable utc offset in the input time string:

>>> from email.utils import parsedate_tz, mktime_tz
>>> from datetime import datetime, timedelta
>>> timestamp = mktime_tz(parsedate_tz('Tue May 08 15:14:45 +0800 2012'))
>>> utc_time = datetime(1970, 1, 1) + timedelta(seconds=timestamp)
>>> utc_time
datetime.datetime(2012, 5, 8, 7, 14, 45)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • This is solid! My string had "GMT-0500" for example and `parsedate_tz` seems to get confused with the "GMT" but a quick `.replace("GMT", "")` and it's perfect! – Ahmed Fasih Dec 05 '20 at 01:23
  • 1
    There is a function in `email.utils` called `parsedate_to_datetime()` that makes convertion to `datetime.datetime` for you – Adam Jenča Feb 09 '22 at 18:26
3

It has discussed many times in SO. In short, "%z" is not supported because platform not support it. My solution is a new one, just skip the time zone.:

    datetime.datetime.strptime(re.sub(r"[+-]([0-9])+", "", "Tue May 08 15:14:45 +0800 2012"),"%a %b %d %H:%M:%S %Y")
wuliang
  • 749
  • 5
  • 7
0
In [117]: datetime.datetime.strptime?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in method strptime of type object at 0x9a2520>
Namespace:      Interactive
Docstring:
    string, format -> new datetime parsed from a string (like time.strptime()).
JosefAssad
  • 4,018
  • 28
  • 37
  • 1
    I tried `datetime.strptime("Tue May 08 15:14:45 +0800 2012","%a %b %d %H:%M:%S %z %Y")`, but Python raises `'z' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'` – xiaohan2012 May 08 '12 at 07:26