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.
RianaSteynAsked:
Who is Participating?
 
Michel PlungjanIT ExpertCommented:
Ok, I DID post the correct answer.

You did not read the complete article (and I did not try to change the code in the downloaded version).

Yes the example was for Australia but there was clear instructions to show how to change it.

Here are the changes and the script

var dst = 0;       // set to 1 for daylight savings time
                   // update this as you go on and off daylight saving time
var loc = 'Durban, South Africa'; // set to your location
var mtz = 2;      // set to your local timezone (hours ahead of UTC, negative if behind)
var stdz = 'SAST'; // standard time indicator
var dayz = ''; // daylight saving time indicator (blank if you dont have daylight saving)



<script>
// display current time at author location
// =======================================
// copyright Stephen Chapman, Felgall Pty Ltd, 11 July 2001, 25th November 2004
// http://www.felgall.com/ and http://javascript.about.com/
// permission is given to use this script
// provided that all comment lines in the script are retained
 
function myTime() {
var dst = 0;       // set to 1 for daylight savings time
                   // update this as you go on and off daylight saving time
 
var loc = 'Durban, Africa'; // set to your location
var mtz = 2;      // set to your local timezone (hours ahead of UTC, negative if behind)
var stdz = 'SAST'; // standard time indicator
var dayz = ''; // daylight saving time indicator (blank if you dont have daylight saving)
 
// do not alter anything below this line
document.writeln('The time in ' + loc + ' is <span id="time">' + setDsp(mtz,dst,stdz,dayz) + '<\/span>');
if (DOMsupported) setTimeout('updDsp('+mtz+',' +dst+',"' +stdz+'","' +dayz+'")', 5000);
}
 
var DOMsupported = 0;var standardDOMsupported = 0;var ieDOMsupported = 0;
if (document.getElementById) { standardDOMsupported = 1; DOMsupported = 1;}
else { if (document.all) {ieDOMsupported = 1; DOMsupported = 1;}
}
function findDOM(objectId) {
if (standardDOMsupported) {return (document.getElementById(objectId));}
if (ieDOMsupported) {return (document.all[objectId]);}
}
 
function updDsp(mtz,dst,stdz,dayz) {
var obj = findDOM('time');
obj.innerHTML = setDsp(mtz,dst,stdz,dayz);
setTimeout('updDsp('+mtz+ ','+dst+ ',"'+stdz+ '","'+dayz+ '")', 5000);
}
function setDsp(mtz,dst,stdz,dayz) {
var dayname = new Array ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday');
var now = new Date;
now.setUTCMinutes(now.getUTCMinutes() + (mtz + dst)*60);
var dow = now.getUTCDay();
var minute = now.getUTCMinutes();
var hour = now.getUTCHours();
if (hour > 11) {ampm = 'PM'; hour -= 12;} else {ampm = 'AM'}
if (hour == 0) {hour = 12;} if (minute < 10) {pad = ':0';} else {pad = ':';}
var txt = hour + pad + minute + ' ' + ampm + ' (';
if (dst) txt += dayz; else txt += stdz;
txt += ') on ' + dayname[dow];
return (txt);
}
 
myTime()
 
</script>

Open in new window

0
 
Sreedhar VengalaSr. Consultant - Business IntelligenceCommented:
0
 
RianaSteynAuthor Commented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Michel PlungjanIT ExpertCommented:
the JS Date object does that. Where do you see the part in the script you pasted that does any additional daylight saving thing?

0
 
RianaSteynAuthor Commented:
I see it in the results that I get.
0
 
Michel PlungjanIT ExpertCommented:
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

0
 
RianaSteynAuthor Commented:
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.
0
 
Michel PlungjanIT ExpertCommented:
So use my function to change the time by one hour if isDst returns true
0
 
Michel PlungjanIT ExpertCommented:
Hmm - I believe we DID give usable answers - did you try my suggestions?
0
 
RianaSteynAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.