Python – Parsing time instants in Python

Parsing time instants in Python… here is a solution to the problem.

Parsing time instants in Python

I’m relatively new to Python.
I have timestamp in format – 2016-12-04T21:16:31.265Z. It is a string type. I would like to know how to parse the above timestamp in python.

I’m looking at the datetime library, but it only seems to accept floats. How do I parse a timestamp? I’m trying to find something similar to Python’s Instant (in Java)?

Solution

import datetime
time_str = '2016-12-04T21:16:31.265Z'
time_stamp = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S.%fZ")
print(time_stamp)

Quote: https://docs.python.org/2/library/datetime.html ; (8.1.7. strftime() and strptime() behavior.)

Related Problems and Solutions