admin管理员组文章数量:1336643
In JavaScript, I can assign:
var now = Date.now();
Then use now to calculate as a number variable
>>> import datetime
>>> datetime.datetime.now()
>>> datetime.datetime.today()
In python3, Using upper code doesnt seem helpful. Any idea what is equivalent of Date.now().
In JavaScript, I can assign:
var now = Date.now();
Then use now to calculate as a number variable
>>> import datetime
>>> datetime.datetime.now()
>>> datetime.datetime.today()
In python3, Using upper code doesnt seem helpful. Any idea what is equivalent of Date.now().
Share Improve this question edited Jul 10, 2021 at 17:57 Machinexa asked Oct 1, 2020 at 13:33 MachinexaMachinexa 5991 gold badge10 silver badges21 bronze badges 3-
1
Start with
datetime.datetime.now().timestamp()
. See tutorialspoint./… – jarmod Commented Oct 1, 2020 at 13:37 - Duplicate of Get current time in milliseconds in Python? – trincot Commented Oct 1, 2020 at 13:50
- Oh didnt realize that – Machinexa Commented Oct 1, 2020 at 13:55
2 Answers
Reset to default 8You should use time
module.
>>> import time
>>> int(time.time() * 1000) #for getting the millisecs
1601559393404
Date.now() returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. The Python 3.3 and later equivalent is:
from datetime import datetime, timezone
now_ms = int(datetime.now(tz=timezone.utc).timestamp() * 1000)
Without tz=timezone.utc
, datetime.timestamp will use a naive datetime, usually set to your local time with timezone, daylight savings time, etc. This can be a subtle source of bugs, when going from Python to Javascript, between development environments and production, or around daylight savings time transitions.
本文标签: python 3xWhat is python3 equivalent of Datenow() of javascriptStack Overflow
版权声明:本文标题:python 3.x - What is python3 equivalent of Date.now() of javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742257308a2441863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论