I'm attempting to create a simple python function which will return the same value as javascript new Date().getTime()
method.
As written here, javascript getTime() method returns number of milliseconds from 1/1/1970
So I simply wrote this python function:
def jsGetTime(dtime):
diff = datetime.datetime(1970,1,1)
return (dtime-diff).total_seconds()*1000
while the parameter dtime is a python datetime object.
yet I get wrong result. what is the problem with my calculation?
getTime() The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.
The date. getTime() method is used to return the number of milliseconds since 1 January 1970. when a new Date object is created it stores the date and time data when it is created. When the getTime() method is called on this date object it returns the number of milliseconds since 1 January 1970 (Unix Epoch).
Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation.
getTime() returns the number of milliseconds since January 1, 1970 00:00:00.
One thing I feel compelled to point out here is: If you are trying to sync your client time and your server time you are going to need to pass the server time to the client and use that as an offset. Otherwise you are always going to be a bit out of sync as your clients/web-browsers will be running on various machines which have there own clock. However it is a common pattern to reference time in a unified manor using epoch milliseconds to sync between the clients and the server.
The Python
import time, datetime
def now_milliseconds():
return int(time.time() * 1000)
# reference time.time
# Return the current time in seconds since the Epoch.
# Fractions of a second may be present if the system clock provides them.
# Note: if your system clock provides fractions of a second you can end up
# with results like: 1405821684785.2
# our conversion to an int prevents this
def date_time_milliseconds(date_time_obj):
return int(time.mktime(date_time_obj.timetuple()) * 1000)
# reference: time.mktime() will
# Convert a time tuple in local time to seconds since the Epoch.
mstimeone = now_milliseconds()
mstimetwo = date_time_milliseconds(datetime.datetime.utcnow())
# value of mstimeone
# 1405821684785
# value of mstimetwo
# 1405839684000
The Javascript
d = new Date()
d.getTime()
See this post for more reference on javascript date manipulation.
Javascript's Date does not work like you expect. But your python code is correct.
According to The epoch time listing, the epoch time for January 1, 2010 should be
1262304000
Python: (appears to be correct)
>>> (datetime(2010,1,1) - datetime(1970,1,1)).total_seconds()
1262304000.0
Javascript (appears to be wrong)
> new Date(2010,1,1).getTime()
1265011200000
or
> new Date(2010,1,1).getTime()/1000
1265011200
This is because Javascript date is not creating the date the way you expect. First, it creates the date in your current timezone, and not in UTC. So a "get current time" in javascript would be the clients time, whereas python would return the utc time. Also note that there is a bug in JS Date where the month is actually 0 based and not 1 based.
> new Date(2010,1,1,0,0,0,0)
Date 2010-02-01T08:00:00.000Z
> new Date(2010,0,1,0,0,0,0)
Date 2010-01-01T08:00:00.000Z
Javascript can create a date from an epoch time:
> new Date(1262304000000)
Date 2010-01-01T00:00:00.000Z
Which is correct.
Alternatively you could use the following JS function to get a more accurate time please note that the month still starts at 0 and not 1
> Date.UTC(2010,0,1)
1262304000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With