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
Asked
Active
Viewed 344 times
0
-
It's been aswered [here](https://stackoverflow.com/questions/19801727/convert-datetime-to-unix-timestamp-and-convert-it-back-in-python) – pyzer Nov 04 '21 at 07:26
-
I'll check that out, thanks! – DreamLogic Nov 04 '21 at 07:27
-
@pyzer it returns the timestamp as a string, how could I work with that? – DreamLogic Nov 04 '21 at 07:33
-
Can you please show us your code? – pyzer Nov 04 '21 at 07:45
-
Does this answer your question? [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date) – moooeeeep Nov 04 '21 at 08:08
-
@moooeeeep msh gave me a similar answer below, thanks though! – DreamLogic Nov 05 '21 at 02:03
1 Answers
0
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()

MSH
- 1,743
- 2
- 14
- 22