I have a date in this format 04-07-2018 the following Javascript produces
const date1 = new Date("04-07-2018").getTime();
date1; // 1523084400000
how do I parse the date and convert them to milliseconds in python
I have a date in this format 04-07-2018 the following Javascript produces
const date1 = new Date("04-07-2018").getTime();
date1; // 1523084400000
how do I parse the date and convert them to milliseconds in python
We can perform this in three steps:
datetime
object; datetime
object and the first of January, 1970;For example:
from datetime import datetime
def total_millis(text):
dt = datetime.strptime(text, '%m-%d-%Y')
return 1000 * (dt - datetime(1970, 1, 1)).total_seconds()
This implementation however is timezone independent, whereas the JavaScript version depends on the timezone.
The JavaScript getTime()
method returns the number of milliseconds since January 1, 1970 00:00:00.
You can use datetime
but you should compute it with a deltatime
object since according to this Python doesn't guarantee any particular epoch time
import datetime
delta = datetime.datetime(2018, 4, 7) - datetime.datetime(1970, 1, 1)
milliseconds = delta.total_seconds()*1000 # 1523059200000.0