ric*_*ick 6 javascript python datetime utc unix-timestamp
在Python中,使用calendar.timegm(),我得到一个unix时间戳的10位数结果.当我把它放到Javscript的setTime()函数中时,它在1970年出现了一个日期.它显然需要一个长度为13位的unix时间戳.怎么会发生这种情况?它们都是从同一天算起的吗?
如何在这两种语言之间使用相同的unix时间戳?
在Python中:
In [60]: parseddate.utctimetuple()
Out[60]: (2009, 7, 17, 1, 21, 0, 4, 198, 0)
In [61]: calendar.timegm(parseddate.utctimetuple())
Out[61]: 1247793660
Run Code Online (Sandbox Code Playgroud)
在Firebug中:
>>> var d = new Date(); d.setTime(1247793660); d.toUTCString()
"Thu, 15 Jan 1970 10:36:55 GMT"
Run Code Online (Sandbox Code Playgroud)
这里有一些我用来转换为javascript/datetime的python方法.
def to_datetime(js_timestamp):
return datetime.datetime.fromtimestamp(js_timestamp/1000)
def js_timestamp_from_datetime(dt):
return 1000 * time.mktime(dt.timetuple())
Run Code Online (Sandbox Code Playgroud)
在javascript中你会做:
var dt = new Date();
dt.setTime(js_timestamp);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3341 次 |
| 最近记录: |