102

I have a datasets where all the dates have the following format:

2012-10-09T19:00:55Z

I'd like to be able to be able to use methods like .weekday on them. How do I convert them to the proper format in Python?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Spearfisher
  • 7,303
  • 17
  • 59
  • 115
  • http://stackoverflow.com/questions/969285/how-do-i-translate-a-iso-8601-datetime-string-into-a-python-datetime-object duplicate? – hd1 Apr 24 '14 at 18:51

3 Answers3

169

You can use dateutil.parser.parse (install with python -m pip install python-dateutil) to parse strings into datetime objects.

dateutil.parser.parse will attempt to guess the format of your string, if you know the exact format in advance then you can use datetime.strptime which you supply a format string to (see Brent Washburne's answer).

from dateutil.parser import parse

a = "2012-10-09T19:00:55Z"

b = parse(a)

print(b.weekday())
# 1 (equal to a Tuesday)
David Jones
  • 3,640
  • 2
  • 28
  • 42
Ffisegydd
  • 43,058
  • 12
  • 125
  • 109
  • 17
    I don't know why I haven't heard of `dateutil.parser`. These are the little things that make Python awesome. `from somemodule import problemsolver && problemsolver.solvemyspecificproblem()` – kraxor Oct 03 '14 at 07:49
  • 22
    @kraxor probably because it's not actually part of Python, rather a 3rd-part library. `git clone http://example.com/module/problemsolver problemsolver && cd problemsolver && python problemsolver.py myspecificproblem` – Jonathan Baldwin Nov 15 '14 at 00:49
  • 2
    @Ffisegydd My mistake, I did 'pip install dateutil' rather than prepending with python-. – Paul Jan 13 '15 at 13:42
  • 15
    You need to indicate that thus us a third party lib – frostymarvelous Jul 09 '15 at 13:37
  • 6
    @kraxor Python is awesome because it the accepted answer on how to parse a date requires installing a 3rd party library? That seems somewhat less than awesome. – Greg Ennis Sep 07 '17 at 13:58
  • Little catch with `parse` is dates like this: `parse("12/11/2017 07:41:27") datetime.datetime(2017, 12, 11, 7, 41, 27)` asumes the first arg is the month `parse("13/11/2017 07:41:27") datetime.datetime(2017, 11, 13, 7, 41, 27)` magically makes the first arg the day. All explainable just something you need to know. – albertjan Nov 21 '17 at 11:25
  • @albertjan That gotcha is not ISO-8601 compliant, so it wouldn't be an issue here. The ISO 8601 standard requires a descending arrangement of terms (whether Y-M-D, Y-M, Y-D, Y-W, etc). Though personally I'd prefer that it raise an exception unless some flag is set to automatically convert non-conforming dates. – cosmicFluke May 16 '18 at 15:22
  • To clarify @Paul's comment, the command to install dateutil with pip is `pip install python-dateutil`. The "python-" part is required! – mactyr Mar 01 '19 at 19:24
92

This has already been answered here: How do I translate a ISO 8601 datetime string into a Python datetime object?

d = datetime.datetime.strptime( "2012-10-09T19:00:55Z", "%Y-%m-%dT%H:%M:%SZ" )
d.weekday()
Community
  • 1
  • 1
Brent Washburne
  • 11,417
  • 4
  • 51
  • 70
  • This is suboptimal, since it requires the caller to provide format strings for the rather wide range of ISO 8601 variants (unless one likes ValueError if the seconds are omitted, e.g). The dateutil parser, on the other hand, handles these much better. – Alex North-Keys Apr 25 '17 at 15:46
  • 9
    @AlexNorth-Keys: How is this suboptimal? The OP stated "I have a datasets where all the dates have the following format" and didn't mention any variants. This answer is actually optimal because it doesn't require any external modules, it uses the built-in datetime module. By using the exact format string (instead of having dateutil guess the format every time), the code performance is optimal. – Brent Washburne Apr 25 '17 at 17:48
  • 4
    I was thinking from a maintenance perspective (oops) - but you're right, the strptime method is faster, running in about 1/6th of the time the dateutil.parser approach does. – Alex North-Keys Apr 26 '17 at 05:02
  • 1
    `"%Y-%m-%dT%H:%M:%SZ"` produces a “naive” datetime. To produce an “aware” datetime (i.e., aware of the time zone): `"%Y-%m-%dT%H:%M:%S%z"` – Walter Tross Oct 07 '19 at 09:45
10

You should have a look at moment which is a python port of the excellent js lib momentjs.

One advantage of it is the support of ISO 8601 strings formats, as well as a generic "% format" :

import moment
time_string='2012-10-09T19:00:55Z'

m = moment.date(time_string, '%Y-%m-%dT%H:%M:%SZ')
print m.format('YYYY-M-D H:M')
print m.weekday

Result:

2012-10-09 19:10
2
Peter Dolan
  • 1,198
  • 10
  • 22
Bruno Duyé
  • 765
  • 9
  • 13