Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting unix timestamp string to readable date

I have a string representing a unix timestamp (i.e. "1284101485") in Python, and I'd like to convert it to a readable date. When I use time.strftime, I get a TypeError:

>>>import time >>>print time.strftime("%B %d %Y", "1284101485")  Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: argument must be 9-item sequence, not str 
like image 442
VeryNewToPython Avatar asked Sep 10 '10 06:09

VeryNewToPython


People also ask

How do I read a Unix timestamp in Python?

DateTime to Unix timestamp in UTC Timezone In the time module, the timegm function returns a Unix timestamp. The timetuple() function of the datetime class returns the datetime's properties as a named tuple. To obtain the Unix timestamp, use print(UTC).

How do I convert Unix time to normal time in Excel?

Since a day contains 86400 seconds (24 hours x 60 minutes x 60 seconds), conversion to Excel time can be done by dividing days by 86400 and adding the date value for January 1st, 1970. When C5 is formatted with the Excel date "d-mmm-yyyy", the date is displayed as 1-Oct-2018.


1 Answers

Use datetime module:

from datetime import datetime ts = int('1284101485')  # if you encounter a "year is out of range" error the timestamp # may be in milliseconds, try `ts /= 1000` in that case print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')) 
like image 145
Michał Niklas Avatar answered Nov 07 '22 19:11

Michał Niklas