Last Updated: February 25, 2016
· declandewet

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.

Say Thanks

1 Response
Add your response


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 ·