Link to home
Start Free TrialLog in
Avatar of RianaSteyn
RianaSteyn

asked on

How do I disable daylight savings in javascript

Is there any way that I can disable the effect of daylight savings in javascript date.get* functions. I don't want it to adjust for daylight savings.
Avatar of Sreedhar Vengala
Sreedhar Vengala
Flag of Australia image

Avatar of RianaSteyn
RianaSteyn

ASKER

I am very new to javascript. I have already looked at the article but it seems to discuss how to adjust for daylight savings.

Perhaps I should explain my problem in more detail. I am using the javascript equivalent for mktime, that I found on the web. I attach the code. This function seems to adjust for DST. How do I change it NOT to adjust for DST.
function mktime() {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: baris ozdil
    // +      input by: gabriel paderni
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FGFEmperor
    // +      input by: Yannoo
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: jakes
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Marc Palau
    // +   improved by: Brett Zamir (http://brettz9.blogspot.com)
    // *     example 1: mktime(14, 10, 2, 2, 1, 2008);
    // *     returns 1: 1201871402
    // *     example 2: mktime(0, 0, 0, 0, 1, 2008);
    // *     returns 2: 1196463600
    // *     example 3: make = mktime();
    // *     example 3: td = new Date();
    // *     example 3: real = Math.floor(td.getTime()/1000);
    // *     example 3: diff = (real - make);
    // *     results 3: diff < 5
    // *     example 4: mktime(0, 0, 0, 13, 1, 1997)
    // *     returns 4: 883609200
    // *     example 5: mktime(0, 0, 0, 1, 1, 1998)
    // *     returns 5: 883609200
    // *     example 6: mktime(0, 0, 0, 1, 1, 98)
    // *     returns 6: 883609200
 
    var no=0, i = 0, ma=0, mb=0, d = new Date(), dn = new Date(), argv = arguments, argc = argv.length;
 
    var dateManip = {
        0: function(tt){ return d.setHours(tt); },
        1: function(tt){ return d.setMinutes(tt); },
        2: function(tt){ var set = d.setSeconds(tt); mb = d.getDate() - dn.getDate(); return set;},
        3: function(tt){ var set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - dn.getFullYear(); return set;},
        4: function(tt){ return d.setDate(tt+mb);},
        5: function(tt){
            if (tt >= 0 && tt <= 69) {
                tt += 2000;
            }
            else if (tt >= 70 && tt <= 100) {
                tt += 1900;
            }
            return d.setFullYear(tt+ma);
        }
        // 7th argument (for DST) is deprecated
    };
 
    for( i = 0; i < argc; i++ ){
        no = parseInt(argv[i]*1);
        if (isNaN(no)) {
            return false;
        } else {
            // arg is number, let's manipulate date object
            if(!dateManip[i](no)){
                // failed
                return false;
            }
        }
    }
    for (i = argc; i < 6; i++) {
        switch(i) {
            case 0:
                no = dn.getHours();
                break;
            case 1:
                no = dn.getMinutes();
                break;
            case 2:
                no = dn.getSeconds();
                break;
            case 3:
                no = dn.getMonth()+1;
                break;
            case 4:
                no = dn.getDate();
                break;
            case 5:
                no = dn.getUTCFullYear();
                break;
        }
        dateManip[i](no);
    }
 
    return Math.floor(d.getTime()/1000);
}

Open in new window

Avatar of Michel Plungjan
the JS Date object does that. Where do you see the part in the script you pasted that does any additional daylight saving thing?

I see it in the results that I get.
The link given sets the dst to true (1) if dst is on. So you can act accordingly

However the function was buggy so I changed it



function isDst(date) {
var gmt = new Date(date.getTime());
var lsm = new Date(date.getTime());
var lso = new Date(date.getTime());
lsm.setMonth(2); // March
lsm.setDate(31);
var day = lsm.getDay();// day of week of 31st
lsm.setDate(31-day); // last Sunday
lso.setMonth(9); // October
lso.setDate(31);
day = lso.getDay();
lso.setDate(31-day);
return (gmt >= lsm && gmt <= lso) 
}
 
alert(isDst(new Date()))

Open in new window

I saw the isDST function in the article but that tests for DST specifically for Australia - so that doesn't help me. We don't have daylight savings so I want to switch DST off if it is possible.
So use my function to change the time by one hour if isDst returns true
Hmm - I believe we DID give usable answers - did you try my suggestions?
The suggestions was not usable. The function you provided came from an article that tests for DST in Australia. It depends on the specific days when DST is switched on and off in Australia. In South Africa we do not have DST. The time functions in Javascript does tha DST adjustment automatically and your suggestion did not reveal the dates or variables upon which it depends. I wanted to know if was possible to disable javascript's DST.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial