Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 914
fuentesjr Avatar asked Oct 18 '08 08:10

fuentesjr


People also ask

What is 000z time?

Where and When is Z Observed? Zulu Time Zone is often used in aviation and the military as another name for UTC +0. Zulu Time Zone is also commonly used at sea between longitudes 7.5° West and 7.5° East. The letter Z may be used as a suffix to denote a time being in the Zulu Time Zone, such as 08:00Z or 0800Z.

What is SSSZ in date format?

Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.


1 Answers

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' 
like image 164
Jeremy Cantrell Avatar answered Oct 05 '22 00:10

Jeremy Cantrell