Organizational Research By

Surprising Reserch Topic

check if daylight saving time is in effect and if it is for how many hours

check if daylight saving time is in effect and if it is for how many hours  using -''

Using JavaScript, how can I check if Daylight Saving Time (DST) is in use at the moment, and if it is for how many hours?

This is a bit of my js code for which this is needed:

var secDiff=Math.abs(Math.round((utc_date-this.premiere_date)/1000));

I want to get the datetime in ago, but if the DST is in use then the dates are wrong for 1 hour, and that's my problem. I don't know how to check if the DST is in use or not.

How to get when daylight saving starts and ends? <--- I think that this could help me. Just can't find it.

asked Oct 13, 2015 by atulpariharmca
0 votes

Related Hot Questions

7 Answers

0 votes

The code given by this article will tell you whether Daylight Savings Time is in effect. It uses the fact that getTimezoneOffset returns a different value during DST and standard time, and compares the difference between the two. (for example New York returns -5 normally and -4 during DST)

Note that I have no idea as to the intricacies of international time zones, and have only tested that it returns correct results for my time zone.. but the code seems solid.

var today = new Date();
if (today.dst()) { alert ("Daylight savings time!"); }

Date.prototype.stdTimezoneOffset = function() {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());

Date.prototype.dst = function() {
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
answered Oct 13, 2015 by nikhilapatil
0 votes

Create two dates: one in June, one in January. Compare their getTimezoneOffset() values.

  • if January offset > June offset, client is in northern hemisphere
  • if January offset < June offset, client is in southern hemisphere
  • if no difference, client timezone does not observe DST

Now check getTimezoneOffset() of the current date.

  • if equal to June, northern hemisphere, then current timezone is DST (+1 hour)
  • if equal to January, southern hemisphere, then current timezone is DST (+1 hour)
answered Oct 13, 2015 by manju bhargava
0 votes

I was faced with this same problem today but since our daylight saving starts and stops at differing times from the USA (at least from my understanding), I used a slightly different route..

var arr = [];
for (var i = 0; i < 365; i++) {
 var d = new Date();
 newoffset = d.getTimezoneOffset();
DST = Math.min.apply(null, arr);
nonDST = Math.max.apply(null, arr);

Then you simply compare the current timezone offset with DST and nonDST to see which one matches.

answered Oct 13, 2015 by thiru
0 votes

Based on Matt Johanson's comment on the solution provided by Sheldon Griffin I created the following code:

    Date.prototype.stdTimezoneOffset = function() {
        var fy=this.getFullYear();
        if (!Date.prototype.stdTimezoneOffset.cache.hasOwnProperty(fy)) {

            var maxOffset = new Date(fy, 0, 1).getTimezoneOffset();
            var monthsTestOrder=[6,7,5,8,4,9,3,10,2,11,1];

            for(var mi=0;mi<12;mi++) {
                var offset=new Date(fy, monthsTestOrder[mi], 1).getTimezoneOffset();
                if (offset!=maxOffset) { 
        return Date.prototype.stdTimezoneOffset.cache[fy];


    Date.prototype.isDST = function() {
        return this.getTimezoneOffset() < this.stdTimezoneOffset(); 

It tries to get the best of all worlds taking into account all the comments and previously suggested answers and specifically it:

1) Caches the result for per year stdTimezoneOffset so that you don't need to recalculate it when testing multiple dates in the same year.

2) It does not assume that DST (if it exists at all) is necessarily in July, and will work even if it will at some point and some place be any month. However Performance-wise it will work faster if indeed July (or near by months) are indeed DST.

3) Worse case it will compare the getTimezoneOffset of the first of each month. [and do that Once per tested year].

The assumption it does still makes is that the if there is DST period is larger then a single month.

If someone wants to remove that assumption he can change loop into something more like whats in the solutin provided by Aaron Cole - but I would still jump half a year ahead and break out of the loop when two different offsets are found]

answered Oct 13, 2015 by deven.bendale
0 votes

Your close but a little off. You never need to calculate your own time as it is a result of your own clock. It can detect if you are using daylight saving time in your location but not for a remote location produced by the offset:

newDateWithOffset = new Date(utc + (3600000*(offset)));

This will still be wrong and off an hour if they are in DST. You need for a remote time account if they are currently inside their DST or not and adjust accordingly. try calculating this and change your clock to - lets say 2/1/2015 and reset the clock back an hour as if outside DST. Then calculate for an offset for a place that should still be 2 hours behind. It will show an hour ahead of the two hour window. You would still need to account for the hour and adjust. I did it for NY and Denver and always go the incorrect (hour ahead) in Denver.

answered Oct 13, 2015 by amit.gupta
0 votes

I recently needed to create a date string with UTC and DST, and based on Sheldon's answer I put this together:

Date.prototype.getTimezone = function(showDST) {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);

    var utcOffset = new Date().getTimezoneOffset() / 60 * -1;
    var dstOffset = (jan.getTimezoneOffset() - jul.getTimezoneOffset()) / 60;

    var utc = "UTC" + utcOffset.getSign() + (utcOffset * 100).preFixed(1000);
    var dst = "DST" + dstOffset.getSign() + (dstOffset * 100).preFixed(1000);

    if (showDST) {
        return utc + " (" + dst + ")";

    return utc;
Number.prototype.preFixed = function (preCeiling) {
    var num = parseInt(this, 10);
    if (preCeiling && num < preCeiling) {
        num = Math.abs(num);
        var numLength		 = num.toString().length;
        var preCeilingLength = preCeiling.toString().length;
        var preOffset		 = preCeilingLength - numLength;
        for (var i = 0; i < preOffset; i++) {
            num = "0" + num;
    return num;
Number.prototype.getSign = function () {
    var num	 = parseInt(this, 10);
    var sign = "+";
    if (num < 0) {
        sign = "-";
    return sign;

document.body.innerHTML += new Date().getTimezone() + "
"; document.body.innerHTML += new Date().getTimezone(true);

Output for Turkey (UTC+0200) and currently in DST:   UTC+0300 (DST+0100)

answered Oct 13, 2015 by rajeevr642
0 votes

Already solved, but just thought that I should add the way I do it (in case people are interested).

 function isDST(t) { //t is the date object to check, returns true if daylight saving time is in effect.
    var jan = new Date(t.getFullYear(),0,1);
    var jul = new Date(t.getFullYear(),6,1);
    return Math.min(jan.getTimezoneOffset(),jul.getTimezoneOffset()) == t.getTimezoneOffset();  

Works the same way as the excepted answer (janurary and july timezone offset), then returns the conditional.

answered Oct 13, 2015 by rajeshujade