Python转换很久以前

Luc*_*man 7 python datetime

我正在尝试将长期转换为日期:

class timeStamp(object):
     def getDateTime(self,longDate):                                                                                                                                 
         myNumber = float(longDate)
         return str(datetime.datetime.fromtimestamp(time.ctime(myNumber)).strftime('%Y-%m-%d %H:%M:%S'))
Run Code Online (Sandbox Code Playgroud)

但我有一个奇怪的错误:

 File "./index.py", line 104, in getDateTime
    return str(datetime.datetime.fromtimestamp(time.ctime(myNumber)).strftime('%Y-%m-%d %H:%M:%S'))
TypeError: a float is required
Run Code Online (Sandbox Code Playgroud)

当我明确地将它转换为浮动时,为什么会抱怨?long是一个在mysql中存储为long的Unix时间戳.

mat*_*ata 10

time.ctime() 为您提供时间的字符串表示.

它应该与:

datetime.datetime.fromtimestamp(myNumber).strftime('%Y-%m-%d %H:%M:%S')
Run Code Online (Sandbox Code Playgroud)


Bur*_*lid 10

你需要的仅仅是 datetime.datetime.fromtimestamp(myNumber).strftime('%Y-%m-%d %H:%M:%S')time.ctime()返回一个字符串:

>>> time.ctime()
'Sat May 19 13:46:09 2012'
Run Code Online (Sandbox Code Playgroud)