我有一个字符串表示unix时间戳(即。"1284101485"),我想把它转换成一个可读的日期。当我利用时间的时候。strftime,我得到一个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

当前回答

你可以使用easy_date来简化:

import date_converter
my_date_string = date_converter.timestamp_to_string(1284101485, "%B %d, %Y")

其他回答

对于来自UNIX时间戳的人类可读时间戳,我以前在脚本中使用过:

import os, datetime

datetime.datetime.fromtimestamp(float(os.path.getmtime("FILE"))).strftime("%B %d, %Y")

输出:

“2012年12月26日”

使用datetime模块:

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'))
import datetime
temp = datetime.datetime.fromtimestamp(1386181800).strftime('%Y-%m-%d %H:%M:%S')
print temp

如果你正在使用一个数据帧,并且不希望系列不能转换为类int错误。使用下面的代码。

new_df= pd.to_datetime(df_new['time'], unit='s')

又快又脏的一句话:

'-'.join(str(x) for x in list(tuple(datetime.datetime.now().timetuple())[:6]))

'the 2013-5-5-2013-5-5'