Converting Epoch time into the datetime Converting Epoch time into the datetime python python

Converting Epoch time into the datetime


To convert your time value (float or int) to a formatted string, use:

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))


You can also use datetime:

>>> import datetime>>> datetime.datetime.fromtimestamp(1347517370).strftime('%c')  '2012-09-13 02:22:50'


>>> import datetime>>> datetime.datetime.fromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')'2012-09-13 14:22:50' # Local time

To get UTC:

>>> datetime.datetime.utcfromtimestamp(1347517370).strftime('%Y-%m-%d %H:%M:%S')  '2012-09-13 06:22:50'