1

I am attempting to convert the following date (2012-12-25T08:52:00-05:00) to a datetime object in python. However, I cannot figure out what the -05:00 part of the date is referencing. I am simply trying to perform the following:

datetime.datetime.strptime('2012-12-25T08:52:00-05:00','%Y-%m-%dT%H:%M:%S')

But this comes up with an expected 'ValueError: unconverted data remains'. I'm just trying to figure out what the last part of the date is used for so that I can convert that string to a proper datetime object in python.

Happy Holidays!

2 Answers 2

5

Your date seems to be in the ISO 8601 format, I don't think datetime handles the timezone information at the end of the string format.

You can use pip install python-dateutil, its parser can return a datetime object :

import dateutil.parser
datestr = '2012-12-25T08:52:00-05:00'
dateutil.parser.parse(datestr)
>>> datetime.datetime(2012, 12, 25, 8, 52, tzinfo=tzoffset(None, -18000)) 
Sign up to request clarification or add additional context in comments.

1 Comment

On UTC-systems parse('1998-01-01T00:00:00Z') gives datetime.datetime(1998, 1, 1, 0, 0, tzinfo=tzlocal()) on non-UTC systems it returns datetime.datetime(1998, 1, 1, 0, 0, tzinfo=tzutc()) - a very odd inconsistency.
0

The -05:00 indicates the timezone offset from UTC, i.e. %z would be the correct strptime argument to parse it.

If the time is UTC the offset might be indicated using Z, e.g. 2012-12-25T08:52:00Z. Not sure if %z would actually accept this...

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.