How to convert a datetime object into a Unix timestamp and back in Python

A datetime object contains the current local date and time, and a Unix timestamp is a float object containing the number of seconds between a particular date and January 1, 1970 at UTC.

Solution for How to convert a datetime object into a Unix timestamp and back in Python : You can use datetime.datetime.timestamp() and datetime.date.fromtimestamp() to convert a datetime object into a Unix timestamp and back Call datetime.datetime(year, month, day) with year, month, and day from a specific date to create a datetime object. Call datetime.timestamp() on the datetime object to generate that object’s Unix timestamp. Call datetime.fromtimestamp(timestamp) with timestamp as the result of the previous step to create a datetime object from it.


how-to-convert-a-datetime-object-into-a-unix-timestamp-and-back-in-python