我有一个字符串表示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

当前回答

我刚刚成功地使用了:

>>> type(tstamp)
pandas.tslib.Timestamp
>>> newDt = tstamp.date()
>>> type(newDt)
datetime.date

其他回答

我刚刚成功地使用了:

>>> type(tstamp)
pandas.tslib.Timestamp
>>> newDt = tstamp.date()
>>> type(newDt)
datetime.date
timestamp ="124542124"
value = datetime.datetime.fromtimestamp(timestamp)
exct_time = value.strftime('%d %B %Y %H:%M:%S')

从带有时间的时间戳中获取可读的日期,还可以更改日期的格式。

使用datetime.strftime(格式):

from datetime import datetime
unixtime = int('1284101485')

# Print with local time
print(datetime.fromtimestamp(unixtime).strftime('%Y-%m-%d %H:%M:%S'))

# Print with UTC time
print(datetime.utcfromtimestamp(unixtime).strftime('%Y-%m-%d %H:%M:%S'))

datetime.fromtimestamp(timestamp):返回与POSIX时间戳对应的本地日期,例如time.time()返回的日期。 datetime.utcfromtimestamp(timestamp):返回与POSIX时间戳对应的UTC日期时间,tzinfo为None。(结果对象是朴素的。)

>>> from datetime import datetime
>>> datetime.fromtimestamp(1172969203.1)
datetime.datetime(2007, 3, 4, 0, 46, 43, 100000)

摘自http://seehuhn.de/pages/pdate

你可以使用easy_date来简化:

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