How can I turn the format yyyy-mm-ddThh:mm:ss to a unix timestamp?

I’m returning information from an API and it uses this format to send timestamps, is there any easy way to return this time as Unix? It returns as a string and all the methods i’ve tried so far don’t work. Thank you for any help

>Solution :

Let’s say somehow you got the time as YYY-mm-ddTHH:MM:SS format.

To convert from string to a time object you can use:

from datetime import datetime

t = datetime.strptime("2021-11-4T10:44:00", "%Y-%m-%dT%H:%M:%S")

Now you have a datetime object which you can use to do as you wish. For example you want to convert it to unix time format (which @pyzer shared as a comment)?


Leave a Reply