Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js timezone independent Date.now()

What is a common way to sync timeStamps across servers and clients in node.js, not dependent on timezone?

e.g., a Date.now() equivalent that would provide the same time on the server and client. Preferably without any node.js modules, or client side libraries.

like image 480
ArkahnX Avatar asked Aug 26 '13 18:08

ArkahnX


People also ask

Is date now () affected by timezone?

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.

What is date now () in JavaScript?

The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

What does new date () return?

Return value Calling the Date() function (without the new keyword) returns a string representation of the current date and time, exactly as new Date().toString() does.


1 Answers

JavaScript timestamps are always based in UTC:

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC.

Date strings from different timezones can have the same timestamp.

var a = "2013-08-26 12:00 GMT-0800";
var b = "2013-08-27 00:00 GMT+0400";

console.log(Date.parse(a) === Date.parse(b)); // true
console.log(Date.parse(a)); // 1377547200000
console.log(Date.parse(b)); // 1377547200000

And, Date.now() should return relatively similar values across systems.

like image 143
Jonathan Lonowski Avatar answered Oct 07 '22 14:10

Jonathan Lonowski