Write a Python program convert a date to timestamp


This program is written in Python and it imports the "time" and "datetime" modules. The "datetime" module provides the "datetime" class, which is used to represent dates and times. The line "now = datetime.datetime.now()" creates a "datetime" object that represents the current date and time.

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(now.timetuple())" converts the "datetime" object to a "struct_time" object and then converts it to the equivalent number of seconds since the epoch. Finally, the line "print(time.mktime(now.timetuple()))" prints the number of seconds since the epoch. This number can be used to represent the current time as a Unix timestamp.

Source Code

import time
import datetime
now = datetime.datetime.now()
print(time.mktime(now.timetuple()))

Output

1664006233.0

Example Programs