56

When translating dates to JSON, javascript is saving dates in this format:

2012-05-29T19:30:03.283Z

However, I am not sure how to get this into a python datetime object. I've tried these:

# Throws an error because the 'Z' isn't accounted for:
datetime.datetime.strptime(obj[key], '%Y-%m-%dT%H:%M:%S.%f')

# Throws an error because '%Z' doesn't know what to do with the 'Z'
#  at the end of the string
datetime.datetime.strptime(obj[key], '%Y-%m-%dT%H:%M:%S.%f%Z')

I believe that javascript is saving the string in official ISO format, so it seems that there should be a way to get python's datetime.strptime() to read it?

3
  • 1
    Remove the % in front of the "Z", I think that should work. Commented May 29, 2012 at 19:43
  • 1
    Doesn't that remove the information that 'Z' conveys? Commented May 29, 2012 at 19:44
  • @ChrisDutrow Z is a character to ignore in the string Commented May 29, 2012 at 19:44

3 Answers 3

76

Try the following format:

%Y-%m-%dT%H:%M:%S.%fZ

For example:

>>> datetime.datetime.strptime('2012-05-29T19:30:03.283Z', '%Y-%m-%dT%H:%M:%S.%fZ')
datetime.datetime(2012, 5, 29, 19, 30, 3, 283000)

The Z in the date just means that it should be interpreted as a UTC time, so ignoring it won't cause any loss of information. You can find this information here: http://www.w3.org/TR/NOTE-datetime

Sign up to request clarification or add additional context in comments.

2 Comments

How would you get rid of the time part of the date? When trying with just %Y-%m-%d or %Y-%m-%dT it doesn't seem to work. Thanks!
Nevermind, figured it out using datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ').strftime("%m/%d/%y")
15

To provide an alternative, if you don't mind installing the python-dateutil package, you can use dateutil.parser.parse. Be advised that the format of the input is guessed by parse; an invalid input can still be interpreted, correctly or otherwise. Guessing the format will however be significantly slower than specifying it explicitly.

Without timezone

If you would rather not have the time zone set, which is perfectly fine if you represent all times internally as UTC only, use:

>>> dateutil.parser.parse('2012-05-29T19:30:03.283Z', ignoretz=True)
datetime.datetime(2012, 5, 29, 19, 30, 3, 283000)

With timezone

Note that unlike datetime.datetime.strptime, this default call to parse automatically preserves the UTC time zone.

>>> import dateutil.parser
>>> dateutil.parser.parse('2012-05-29T19:30:03.283Z')
datetime.datetime(2012, 5, 29, 19, 30, 3, 283000, tzinfo=tzutc())

If a test assertion for equality needs to be made, the expected object can be constructed as:

>>> import datetime
>>> datetime.datetime(2012, 5, 29, 19, 30, 3, 283000, tzinfo=dateutil.tz.tzutc())

1 Comment

Preserving the time zone is important. I prefer your answer.
1
import datetime

# Assuming you have a string in the format
date_string = "2012-05-29T19:30:03.283Z"

# You can build a datetime object with
date_object = datetime.fromisoformat(date_string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.