2

Is there any library for JavaScript which I can use to check if the day light saving is in effect for a specific time in a specific location.

For example, I would like to know for a date, say "July 1 05:30:00 UTC+0500 2009" in the time zone code, for example 110, if the day light saving is in effect. Additional information is always acceptable.

The time zone codes can be found here - http://msdn.microsoft.com/en-us/library/bb887715.aspx

Thanks for your help in advance.

Greets!

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Santro
  • 55
  • 5
  • 1
    Check out this post: http://stackoverflow.com/questions/11887934/check-if-daylight-saving-time-is-in-effect-and-if-it-is-for-how-many-hours – bitoffdev Nov 05 '13 at 14:04
  • Thanks for answering. The problem in my case is fairly simple. The time zone of the server is different from the place where I am trying to access it. I need to know if the place where the server is located has day light saving in effect for a given time. Thanks again. – Santro Nov 05 '13 at 14:16
  • Is there some reason you are using MS Dynamics CRM time zone codes? If so, why do you need a *javascript* answer?? – Matt Johnson-Pint Nov 06 '13 at 03:38
  • You may also want to read [this question](http://stackoverflow.com/q/17083233/634824) about how to obtain a regular `TimeZoneInfo` identifier from a CRM time zone code. But still, you should do all that in .net, not in javascript. – Matt Johnson-Pint Nov 06 '13 at 03:51
  • Thanks for trying to help me. I am reading the meta data from CRM into my Javascript. The big problem - all I have is only the Time zone code. I simply need to know for a given time, if the daylight saving is in effect for the time zone from CRM. and yes, I have to do this in Javascript. Thanks again. – Santro Nov 06 '13 at 08:45

2 Answers2

1

Javascript doesn't know anything about CRM time zone codes.

There aren't even any good libraries for JavaScript that can work with Windows time zone identifiers, like the ones you'd find when using TimeZoneInfo from .NET, or browsing through your Windows registry.

If you need to work with time zones in JavaScript, you'll need to convert to an IANA/Olson time zone identifier, and then use one of the libraries I mentioned here.

If you can work in .NET on the server, you could use this method to convert from a CRM Time Zone Id to a Windows Time Zone Id. You could then use this method to convert from Windows to IANA time zones. But if you're going to do that much work on the server anyway, I don't see why you wouldn't just do your data conversions there also.

If you're looking for a pure JavaScript solution that will work directly from the CRM time zone IDs, I'm sorry but as far as I know, that doesn't exist. You'd have to build it yourself and maintain it as timezone data changes.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

This will return true or false when given a timezone as input:

function stdTimezoneOffset() {
    var d = new Date();
    var jan = new Date(d.getFullYear(), 0, 1);
    var jul = new Date(d.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
function dst(offset) {
    var d = new Date(); // create Date object for the current location
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000); // get UTC time in milliseconds
    var nd = new Date(utc + (3600000 * offset)); // Create net Date() for city with offset
    return d.getTimezoneOffset() < stdTimezoneOffset();
}
alert(dst('-5.0')); //New York
bitoffdev
  • 3,134
  • 1
  • 13
  • 16
  • 1
    This is flawed. You are making two common mistakes. 1) you can't identify a time zone from its offset alone. `-5.0` is not just for New York. 2) DST works differently all over the world. The best that this code would do is use your own local time zone's idea of DST and erroneously apply it elsewhere. You should read the "timezone != offset" section of the [timezone tag wiki](http://stackoverflow.com/tags/timezone/info) and also [the dst tag wiki](http://stackoverflow.com/tags/dst/info). – Matt Johnson-Pint Nov 06 '13 at 03:48