Write a Python program to convert a date to Unix timestamp


This program is written in Python and it imports the "datetime" and "time" modules. The "datetime" module provides the "datetime" class, which is used to represent dates and times. The line "dt = datetime.datetime(2022, 10, 5, 23, 23)" creates a "datetime" object that represents the date and time "October 5th, 2022, 11:23 PM" . The "datetime" class takes the year, month, day, hour, and minute as arguments.

The "time" module provides the "mktime" function, which converts a "struct_time" object, as returned by the "gmtime" or "localtime" functions, to seconds since the epoch (the start of 1970). The line "time.mktime(dt.timetuple())" converts the "datetime" object "dt" to a "struct_time" object and then converts it to the equivalent number of seconds since the epoch.

Finally, the line "print("Unix Timestamp: ",(time.mktime(dt.timetuple())))" prints the number of seconds since the epoch for the date and time represented by the "datetime" object "dt" . This number can be used to represent the date and time as a Unix timestamp.

Source Code

import datetime
import time
dt = datetime.datetime(2022, 10, 5, 23, 23)
print("Unix Timestamp: ",(time.mktime(dt.timetuple())))

Output

Unix Timestamp:  1664992380.0

Example Programs