Python @ DjangoSpin

PyPro #41 Current Datetime to Unix Timestamp

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Current Datetime to Unix Timestamp in Python

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

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply