Reading Time: 1 minutes
Current Datetime to Unix Timestamp in Python
Current Datetime to Unix Timestamp: $ date +%s in Unix gives total number of seconds that have elapsed since midnight of Jan 1, 1970. For example, $ date +%s right now is 1488728455. Write a Python program to convert current date & time to a Unix timestamp.
Current Datetime to Unix Timestamp
import datetime import time currentDateTime = datetime.datetime(2017, 3, 5, 21, 10, 55) # year, month, day, hours, minutes, seconds print(currentDateTime) # datetime.datetime(2017, 3, 5, 21, 10, 55) print(time.mktime(currentDateTime.timetuple())) # 1488728455.0 print(int(time.mktime(currentDateTime.timetuple()))) # 1488728455