Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to compare JavaScript's getTime() across different systems?

Tags:

javascript

JavaScript's getTime() returns "the number of milliseconds since 1 January 1970 00:00:00 UTC".

Can I rely on this being similar across different machines? I don't need it to be accurate to the millisecond, just to a few seconds.

Or do I need to use an external time service API, as in this question?

Where does JavaScript get the current time from - is it dependent on the machine's clock?

like image 677
Richard Avatar asked Dec 01 '11 13:12

Richard


People also ask

What does getTime() do?

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.

How to compare two epoch time in js?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.

Is JavaScript getTime UTC?

getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

What does Date getTime return?

JavaScript Date getTime() getTime() returns the number of milliseconds since January 1, 1970 00:00:00.


1 Answers

Can I rely on this being similar across different machines?

No.

Where does JavaScript get the current time from

The system datetime on which this javascript runs.

Or do I need to use an external time service API, as in this question?

You could use the server's time and send it to the client.

like image 51
Darin Dimitrov Avatar answered Sep 22 '22 11:09

Darin Dimitrov