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
Add a ment  | 

2 Answers 2

Reset to default 8

You 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