0
var d = new Date();
document.getElementById('time').innerHTML = d.getHours() + ':' + d.getMinutes();

When I view the code in a browser from my home in London, I see the UTC+1 time:

<div id="time">01:07</div>

I'm pretty sure I see UTC+1 time because it is the time of my system's clock. For somebody viewing the code in Florida, the time would read 20:07 (UTC-4).

How can I force the time output to be UTC+1 for everyone?

henrywright
  • 10,070
  • 23
  • 89
  • 150
  • 1
    You might want to look into [moment-timezone.js](http://momentjs.com/timezone/) which has is a JS library with some fancy timezone-handling capabilities. – Sumner Evans May 12 '15 at 00:15
  • I was thinking about using the moment-timezone.js library but would like to try to understand how to do this using JS if possible. – henrywright May 12 '15 at 00:16
  • 1
    I agree with the momentjs comment, we use it in production, its a great help. – Des Horsley May 12 '15 at 00:17
  • 1
    Maybe watching [this YouTube video from Computerphile about dealing with timezones](https://www.youtube.com/watch?v=-5wpm-gesOY) will convince you to use a 3rd party library. :) – Sumner Evans May 12 '15 at 00:21
  • OK at about 3:26s into the video I've decided I'm using moment.js :) – henrywright May 12 '15 at 00:30

1 Answers1

1

With moment.js timezone

document.getElementById('time').innerHTML = moment().tz('Europe/London').format('HH:mm');
Alex
  • 1,689
  • 18
  • 27