Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)?

t.timestamp()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading