python function to return javascript date.getTime()
Asked Answered
F

2

7

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?

Finnougrian answered 18/7, 2014 at 16:22 Comment(5)
Not an answer to your question, but time.time() does the same thing. Also, if the total seconds is what you're after, why are you multiplying by 1000? I didn't think total_seconds() returned milliseconds.Motorboat
@TheSoundDefense: total milliseconds, not seconds.Phocomelia
@MartijnPieters whoops, turns out I can't read.Motorboat
@TheSoundDefense: and time.time() does not do the same thing; this function returns the offset from the epoch for a given datetime object, while time.time() only gives you the current timestamp.Phocomelia
@TheSoundDefense: I get that a lot too. :-PPhocomelia
C
10

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.

Corybantic answered 18/7, 2014 at 18:2 Comment(3)
Actually I do not try to sync server and clients time. And at the moment I do not want to take care for various time zones. I'm attempting to do a python function which will return the same as javascript getTime(), as if they were both running on the same machine.Finnougrian
Which is exactly what I answered. i.e. python: import time; int(time.time() * 1000) returns 1405793073980L and javascript: d = new Date(); d.getTime() returns 1405793121544Corybantic
I need this for a given datetime. time.time() only gives me timestamp from current time.Finnougrian
M
1

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
Misshape answered 18/7, 2014 at 17:28 Comment(1)
So, if I understood correctly, javascript creates dates based on client timezone, and my python calculation does not. So, how can I get with python the local timezone offset, considering daylight saving time?Finnougrian

© 2022 - 2024 — McMap. All rights reserved.