7

Possible Duplicate:
How to parse ISO formatted date in python?

I have an isoformat datetime as a string like the example below -

'2011-05-31T04:35:33.127-05:00'

What is the best way convert it to a python datetime object? I came across some posts that tell how to get an isoformat string but not the other way round.

Thanks!!

Edit based on Yan's comment-

>>>import dateutil.parser
>>> d1='2011-05-31T04:35:33.127-05:00'
>>> d2=dateutil.parser.parse(d1)
>>> d2
datetime.datetime(2011, 5, 31, 4, 35, 33, 127000, tzinfo=tzoffset(None, -18000))

I need to get the datetime object for the local time represented by the original string. Since I do not know which timezone the input date was in, I cannot use the astimezon method. What will be the best way to get that? Thanks!!

Community
  • 1
  • 1
Rinks
  • 1,007
  • 2
  • 16
  • 22

1 Answers1

0

There is an strptime function, just like in C. http://docs.python.org/library/datetime.html#datetime.datetime.strptime

thule
  • 4,034
  • 21
  • 31
  • Thanks letibee for your response, but unfortunately datetime.datetime.strptime does not work well for the isoformat date string. Thanks Yan, yes I want something like dateutil parser. I have edited my original question. Is there a way to the local time using tzoffset? – Rinks Jun 23 '11 at 22:52