0

I'm in the middle of writing a website, and I got this marvellous idea. I would have my contact page change depending on the time zone it was viewed from.

Examples:

When viewed in the local time-zone (GMT +1):

I can be reached between 08:30 and 17:30

When viewed in New York (GMT -5):

I can be reached between 03:30 and 12:30

In San Francisco (GMT -8):

I can be reached between 00:30 and 09:30

In Beijing (GMT +8):

I can be reached between 16:30 and 01:30

... You get the idea.

I'm not sure where I should start. I know of moment.js but I'm not sure if it's suitable for this purpose, 'specially considering the fact that I want this to be automatically determined by the user's current location.

I'm using Node.js with Express.js for the server, and jQuery on the client-side. Any help?

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131

1 Answers1

1

You can create Date object with UTC time and get local time from it.

// args are in UTC time
function toLocalTime(hours, minutes){
    var d = new Date(Date.UTC(0, 0, 1, hours, minutes, 0));
    return [d.getHours(), d.getMinutes()].map(function(x){
        return ('0' + x).slice(-2);
    }).join(':');
}

"I can be reached between " + toLocalTime(07, 30) + " and " + toLocalTime(16, 30)
tetsurom
  • 113
  • 1
  • 7
  • Oh, I missed about DST. This question looks helpful: http://stackoverflow.com/questions/11887934/check-if-daylight-saving-time-is-in-effect-and-if-it-is-for-how-many-hours – tetsurom Jul 31 '15 at 03:00