Write a Python program convert a string date to the timestamp


This program is written in Python and it imports the "time" and "datetime" modules. The line "s = "08/06/2014"" sets the string variable "s" to the string "08/06/2014" . This string represents a date in the format "dd/mm/yyyy" .

The "datetime" module provides the "datetime" class and the "strptime" method, which can be used to parse a string representation of a date and time and convert it to a "datetime" object. The line "datetime.datetime.strptime(s,"%d/%m/%Y")" uses the "strptime" method to parse the string "s" and convert it to a "datetime" object that represents the date "08/06/2014" .

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(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())" converts the "datetime" object, obtained by parsing the string "s" , to a "struct_time" object and then converts it to the equivalent number of seconds since the epoch.

Finally, the line "print(time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple()))" prints the number of seconds since the epoch for the date represented by the string "s" . This number can be used to represent the date as a Unix timestamp.

Source Code

import time
import datetime
s = "08/06/2014"
print(time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple()))

Output

1402165800.0

Example Programs