2

I get the date and time as string like 2014-05-18T12:19:24+04:00

I found another question explaining how to handle dates in UTC timezone (2012-05-29T19:30:03.283Z)

What should I do with +04:00 in my case (if I want to store time in UTC timezone in Python)?

Upd. I've tried to parse it like below:

dt = '2014-05-19T14:48:50+04:00'
plus_position = dt.find('+') # remove column in the timezone part
colon_pos = dt.find(':', plus_position)
dt = dt[:colon_pos] + dt[colon_pos+1:]
dt = datetime.datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S%z') # '2014-05-19T14:48:50+0400'

But it fails - 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S%z'

1
  • datetime.isoformat produces a timezone with a colon. %z expects a timezone without a colon. Hahaha. Ha. =( Commented May 18, 2014 at 14:34

1 Answer 1

3

Using dateutil:

>>> import dateutil.parser
>>> dateutil.parser.parse('2014-05-18T12:19:24+04:00')
datetime.datetime(2014, 5, 18, 12, 19, 24, tzinfo=tzoffset(None, 14400))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but can I do the same without dateutil? It requires six to be installed... I don't like to use many 3rd party modules with GAE environment.

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.