w3resource

Python: Convert a date to Unix timestamp

Python Datetime: Exercise-35 with Solution

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

Sample Solution:

Python Code:

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

Sample Output:

Unix Timestamp:  1456422780.0 

Flowchart:

Flowchart: Convert a date to Unix timestamp.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

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 Python skills with w3resource's quiz



Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()