103

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?

1

3 Answers 3

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)
Sign up to request clarification or add additional context in comments.

8 Comments

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 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
@Ffisegydd My mistake, I did 'pip install dateutil' rather than prepending with python-.
You need to indicate that thus us a third party lib
@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.
|
94

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()

4 Comments

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.
@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.
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.
"%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"
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.