我这样做:
timestamp=long('1455873250789')
print(timestamp)
d=datetime.datetime(timestamp)
Run Code Online (Sandbox Code Playgroud)
我明白了:
1455873250789
Traceback (most recent call last):
File ".../pycharm-5.0.4/helpers/pydev/pydevd.py", line 2411, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File ".../pycharm-5.0.4/helpers/pydev/pydevd.py", line 1802, in run
launch(file, globals, locals) # execute the script
File "....py", line ..., in <module>
d=datetime.datetime(timestamp)
OverflowError: signed integer is greater than maximum
Run Code Online (Sandbox Code Playgroud)
为什么?
用途datetime.datetime.fromtimestamp:
>>> datetime.datetime.fromtimestamp(timestamp / 1000.0)
datetime.datetime(2016, 2, 19, 18, 14, 10, 789000)
Run Code Online (Sandbox Code Playgroud)
注意:timestamp在传递给方法之前应该除以1000,因为给定时间戳的单位是毫秒,同时fromtimestamp接受秒(这是官方的UNIX时间戳).