Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Date.now and Date.getTime in Javascript and how timezone affects them?

I am trying to understand how creation of dates using Javascript on client and server both can affect query results when timezone of client and server can differ.

On client side, I am using Date.now() to save timestamp information

var time = Date.now()
console.log(time);

The now() method returns the milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now as a Number.

My understanding is that this timestamp is independent of any timezone offset, in other words it points to time at Zero timezone offset. Is it correct?.

We can also get this timestamp using the following code

var now = new Date();
time = now.getTime();
console.log(time);

This code still gives the same timestamp information (at zero timezone offset). Is Date.now() just a quick way to get timestamp?

Now, constructing date from Date.now() and displaying it in browser's console.

var time = Date.now()
console.log(time);

var date = new Date(time)
var time = date.getTime()
console.log(time);

console.log(date);

In my opinion, it is only when displaying in browser's console, date is formatted in the browser's local timezone otherwise this date instance still holds timestamp at zero timezone offset. Is it correct?.

I have also seen an answer of converting Javascript date to UTC

var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
time = now_utc.getTime();
console.log(time);

This code still gives the same timestamp information as given by Date.now() and console.log statements were differing by few milliseconds only which I think is due to code execution time instead of any timezone difference. When executed, following was the output for that instance of time:

1468408292575
1468388492000

Is there any difference between a UTC timestamp and timestamp given by Date.now()?

Can you please confirm if these three options produce same result (i.e. independent of any timezone) and if it is safe to use this timestamp information for client and server interactions when they are in a different timezone. Examples:

  • Client sends a save request to server side to save this client side generated timestamp?
  • Client asks for results based on this saved timestamp?

I want to reduce this to a level where I want to show timezone information only for display purpose e.g. in HTML, in generated files.

My questions might be too simple because I am confused on seeing the Date class documentation on correctly using it to avoid time difference issues.

like image 674
Ritesh Jagga Avatar asked Jul 13 '16 12:07

Ritesh Jagga


2 Answers

I posted an answer on date and timezone related question. Answer says that the internal recording of the date generated on client side are at zero timezone offset. Just adding answers to individual questions:

My understanding is that this timestamp is independent of any timezone offset, in other words it points to time at Zero timezone offset. Is it correct?.

Yes

This code still gives the same timestamp information (at zero timezone offset). Is Date.now() just a quick way to get timestamp?

Yes

Is there any difference between a UTC timestamp and timestamp given by Date.now()?

No

like image 157
Ritesh Jagga Avatar answered Nov 15 '22 00:11

Ritesh Jagga


both are typically same, but Date.now() is little faster

Further Reading:

  • Performance - Date.now() vs Date.getTime()
  • Performance .now() vs Date.now()
like image 44
Jishnu KM Avatar answered Nov 15 '22 01:11

Jishnu KM