2

how to convert this date string to "2011-02-15T12:00+00:00" python datetime object in following format "Wed, Feb, 15, 2011 15:00" ?

Shaan
  • 21
  • 1

2 Answers2

7

It seems ISO 8601 format. Try using iso8601 package — you can install it through pip or easy_install.

Many file formats and standards use the ISO 8601 date format (e.g. 2007-01-14T20:34:22+00:00) to store dates in a neutral, unambiguous manner. This simple module parses the most common forms encountered and returns datetime objects.

>>> import iso8601
>>> iso8601.parse_date("2007-06-20T12:34:40+03:00")
datetime.datetime(2007, 6, 20, 12, 34, 40, tzinfo=<FixedOffset '+03:00'>)
>>> iso8601.parse_date("2007-06-20T12:34:40Z")
datetime.datetime(2007, 6, 20, 12, 34, 40, tzinfo=<iso8601.iso8601.Utc object at 0x100ebf0>)
Community
  • 1
  • 1
minhee
  • 5,688
  • 5
  • 43
  • 81
0

Considering that you know the exact format of the date string, you can parse it to extract each value.

I'm not sure what the +00:00 part means, so I'll ignore that for now.

str="2011-02-15T12:00+00:00"
year=int(str[:4])
month=int(str[5:7])
day=int(str[8:10])
hour=int(str[11:13])
minute=int(str[14:16])
date = datetime(year,month,day,hour,minute)
matzahboy
  • 3,004
  • 20
  • 25