DateTime string to Unix timestamp
The date and time are supplied in string format in this case. Here, 8 denotes the month, 6 denotes the day, 2021 denotes the year, 05 denotes the hour, 54 denotes the minute, and 8 denotes the second. strptime() is a datetime module method that is used to convert strings to datetime and time objects. The timestamp() function returns the current time in the current location.
Code:
Python3
import datetime date_example = "8/6/2021, 05:54:8" date_format = datetime.datetime.strptime(date_example, "%m/%d/%Y, %H:%M:%S" ) unix_time = datetime.datetime.timestamp(date_format) print (unix_time) |
Output:
1628209448.0
How to Convert DateTime to UNIX Timestamp in Python ?
The Unix timestamp is a single signed integer that grows by one every second, allowing computers to store and manipulate conventional date systems. The software is then translated into a human-readable format. The Unix timestamp is the number of seconds calculated since January 1, 1970. In this article, we are going to see how to convert DateTime to Unix timestamp.