For example, I'm trying to convert 2008-09-26T01:51:42.000Z to 09/26/2008. What's the simplest way of accomplishing this?
Asked
Active
Viewed 1e+01k times
5 Answers
58
The easiest way is to use dateutil.parser.parse() to parse the date string into a timezone aware datetime object, then use strftime() to get the format you want.
import dateutil.parser
d = dateutil.parser.parse('2008-09-26T01:51:42.000Z')
print(d.strftime('%m/%d/%Y')) #==> '09/26/2008'

Jeremy Cantrell
- 26,392
- 13
- 55
- 78
-
Why `import datetime`? You don't seem to be using it. – jhrr Nov 16 '17 at 15:50
-
@jhrr `dateutil.parser.parse` converts the string object to datetime object. That `datetime` library is to handle datetime-string conversion fo this object ie. `d.strftime`. – addicted Mar 08 '18 at 03:33
48
I know this is really old question, but you can do it with python datetime.strptime()
>>> from datetime import datetime
>>> date_format = "%Y-%m-%dT%H:%M:%S.%fZ"
>>> datetime.strptime('2008-09-26T01:51:42.000Z', date_format)
datetime.datetime(2008, 9, 26, 1, 51, 42)

mhoang
- 481
- 4
- 3
-
1Thanks for giving the format string. Useful for both strptime and strftime! – Martin Burch Feb 21 '17 at 16:06
-
An even shorter format to do it is using directives: `"%Y-%m-%dT%X.%fZ"` :-) – Mangohero1 Sep 05 '18 at 16:04
24
>>> import time
>>> timestamp = "2008-09-26T01:51:42.000Z"
>>> ts = time.strptime(timestamp[:19], "%Y-%m-%dT%H:%M:%S")
>>> time.strftime("%m/%d/%Y", ts)
'09/26/2008'
See the documentation of the Python time
module for more information.

Greg Hewgill
- 951,095
- 183
- 1,149
- 1,285
-
I know that in this case they are not relevant, of course, but is there a way to handle milliseconds short of stripping them? – Vinko Vrsalovic Oct 18 '08 at 09:04
-
Unfortunately, the strptime function doesn't seem to have a conversion operator for milliseconds. – Greg Hewgill Oct 18 '08 at 09:05
-
Although the answer seems quite complete, if you want to read more on the python/date-time subject I'd suggest this page http://pleac.sourceforge.net/pleac_python/datesandtimes.html – Seldaek Oct 18 '08 at 09:07
-
1While this solution does get the desired result, it does not take into account the timezone (in this case UTC). This could be undesirable in some cases. – Jeremy Cantrell Oct 19 '08 at 03:13
-
Jeremy: the OP didn't ask anything about local time conversion. The solution above manipulates the components of the timestamp without regard to time zone. – Greg Hewgill Oct 19 '08 at 03:30
-
It appears the Python3 does handle microseconds, see the [datetime docs](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior). I am parsing this exact format with `strptime` and the format `%Y-%m-%dT%H:%M:%S.%fZ`. – Jon Surrell Apr 15 '14 at 07:56
3
2008-09-26T01:51:42.000Z
is an ISO8601 date and the format can be very diverse. If you want to parse these dates see the python wiki on working with time. It contains some useful links to modules.

olt
- 2,267
- 1
- 19
- 13
-3
def datechange(datestr):
dateobj=datestr.split('-')
y=dateobj[0]
m=dateobj[1]
d=dateobj[2]
datestr=d +'-'+ m +'-'+y
return datestr
U can make a function like this which take date object andd returns you date in desired dateFormat....

Dhaval dave
- 172
- 2
- 10