问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/24829726/python-function-to-return-javascript-date-gettime