以下代码返回'2012-06-07 00:00'时间戳1339119900000和1339120800000:
>>> from datetime import date
>>> date.fromtimestamp(1339119900000/1e3).strftime('%Y-%m-%d %H:%M')
'2012-06-07 00:00'
>>> date.fromtimestamp(1339120800000/1e3).strftime('%Y-%m-%d %H:%M')
'2012-06-07 00:00'
Run Code Online (Sandbox Code Playgroud)
但是,这些时间戳相隔15分钟,而且它们都不是午夜.
我在Windows 7机器上运行32位Python 2.7.3,但在Red Hat机器上注意到了相同的情况.为什么这样,我如何从时间戳中获得小时和分钟的分辨率?
You are creating date objects, not datetime. Dates ignore all time information.
Use datetime objects instead if you want to preserve the time component:
>>> from datetime import datetime
>>> datetime.fromtimestamp(1339119900000/1e3).strftime('%Y-%m-%d %H:%M')
'2012-06-08 02:45'
>>> datetime.fromtimestamp(1339120800000/1e3).strftime('%Y-%m-%d %H:%M')
'2012-06-08 03:00'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3075 次 |
| 最近记录: |