Python Tutorial

Thursday, October 18, 2012

Python datetime to timestamp two way conversion


It is very nice to having datetime to timestamp two way conversion. Python simply give us this feature. Lets have a look

 
from datetime import datetime
import time

date= datetime(2012,10,16) # You set date as datetime.now()
print date
print time.mktime(date.timetuple())
print datetime.fromtimestamp(time.mktime(date.timetuple()))


Output:
2012-10-16 00:00:00
1350324000.0
2012-10-16 00:00:00

0 comments:

Post a Comment