9

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.

Juergen
  • 12,378
  • 7
  • 39
  • 55
ArkahnX
  • 405
  • 1
  • 5
  • 9
  • 2
    This thread may be of interest: [The best way to synchronize client-side javascript clock with server date](http://stackoverflow.com/questions/1638337/the-best-way-to-synchronize-client-side-javascript-clock-with-server-date) – dc5 Aug 26 '13 at 18:53
  • @dc5 I ended up using something similar to that thread, but I greatly simplified it. – ArkahnX Aug 27 '13 at 19:52

1 Answers1

18

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.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199