'What is python3 equivalent of Date.now() of javascript?
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().
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jwhitlock |
