Python: Convert a date to Unix timestamp

Python Datetime: Exercise-35 with Solution

Write a Python program to convert a date to a Unix timestamp.

Sample Solution:

Python Code:

import datetime
import time
dt = datetime.datetime(2016, 2, 25, 23, 23)
print("Unix Timestamp: ",(time.mktime(dt.timetuple())))

Sample Output:

Unix Timestamp:  1456422780.0 


Flowchart: Convert a date to Unix timestamp.

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to display the date and time in a human-friendly string.
Next: Write a Python program to calculate two date difference in seconds.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.