0

I have a page which displays the local time for 10 cities around the world. However, because of the Daylight Saving Time (DST) one of the times is out by an hour.

Using JavaScript, I want to be able to check for DST to ensure that the times displayed are always correct. This cannot be client specific as this page is used by people around the world.

Here is a bit of the code that I'm currently using:

    var today = new Date();
    today = convertDateToUTC(today);

    var houston = today.addHours(-6);
    var houstonHours = houston.getHours();
    var houtstonMinutes = houston.getMinutes();
    var houstonTime = houstonHours + ":" + houtstonMinutes;
    document.getElementById('houston').innerHTML = houstonTime;
    today = new Date();
    today = convertDateToUTC(today);

    function convertDateToUTC(date) {
         return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
    }
thatguy
  • 23
  • 1
  • 9
  • http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices?rq=1 – briosheje Mar 12 '15 at 08:39
  • There are APIs like Google TimeZone that will tell you whether DST is in effect in a given location. – Barmar Mar 12 '15 at 08:42

1 Answers1

0

Can you use a 3rd party library or does it have to be vanilla? If the former, try moment.js. Specifically: http://momentjs.com/docs/#/query/is-dst-shifted/

Marco Lüthy
  • 1,279
  • 1
  • 11
  • 21
  • I've tried `moment.tz(Date.now(), "America/Indiana/Knox").isDST()` but it always returns false – thatguy Mar 12 '15 at 10:28
  • Moment might be returning false if the Timezone data for 'America/Indiana/Knox' hasn't been loaded. You can confirm that the timezone data is loaded like this `moment.tz.zone('America/Indiana/Knox')` – Marco Lüthy Mar 12 '15 at 10:39