How do you convert YYYY-MM-DDTHH:mm:ss.000Z time format to MM/DD/YYYY time format in Python?

Question:

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 By: fuentesjr

||

Answers:

>>> 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.

Answered By: Greg Hewgill

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.

Answered By: olt

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'
Answered By: Jeremy Cantrell
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….

Answered By: Dhaval dave

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)
Answered By: mhoang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.