r9tmiq
Last Updated: February 25, 2016
·
9.762K
· declandewet
B44a5f76a24cbdca55886ade6d39c596

Convert Node.js "new Date()" to user's local timezone on the client

This is a snippet I have become very fond of. Assuming your node.js server simply sets a time property to new Date() and makes that property available to the client, you can convert it to the user's local time zone using client-side code like this:

var toLocalTime = function(time) {
  var d = new Date(time);
  var offset = (new Date().getTimezoneOffset() / 60) * -1;
  var n = new Date(d.getTime() + offset);
  return n;
};

Couple that with moment.js and all your time-related issues are but a mere memory of a long and forgotten pain.

1 Response
Add your response

15609
8bc62135d7ec7d850048297277929b60

So right now I use:
client.sendMSG( new Date().toLocaleTimeString() + ' : ' + message.utf8Data );

How can I use your function with what I have to get the local time?

over 1 year ago ·