Date Time Formats in Python

user1579970 picture user1579970 · Jul 11, 2013 · Viewed 48.9k times · Source

What are these date-time formats? I need to convert them to the same format, to check if they are the same. These are just two coming from separate data source, so I need to find a way to make them the same format. Any ideas?

2013-07-12T07:00:00Z

2013-07-10T11:00:00.000Z

Thanks in advance

Answer

Sudipta picture Sudipta · Jul 11, 2013

That extra .000 is micro seconds.

This will convert a date string of a format to datetime object.

import datetime
d1 = datetime.datetime.strptime("2013-07-12T07:00:00Z","%Y-%m-%dT%H:%M:%SZ")
d2 = datetime.datetime.strptime("2013-07-10T11:00:00.000Z","%Y-%m-%dT%H:%M:%S.%fZ")

Then convert them into any format depending on your requirement, by using:

new_format = "%Y-%m-%d"
d1.strftime(new_format)