Solved

How do I disable daylight savings in javascript

Posted on 2009-04-08
11
1,136 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:RianaSteyn
  • 5
  • 4
11 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24095830
0
 

Author Comment

by:RianaSteyn
ID: 24095885
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24096361
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:RianaSteyn
ID: 24096382
I see it in the results that I get.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24096431
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
 

Author Comment

by:RianaSteyn
ID: 24104990
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24105123
So use my function to change the time by one hour if isDst returns true
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24599096
Hmm - I believe we DID give usable answers - did you try my suggestions?
0
 

Author Comment

by:RianaSteyn
ID: 24599206
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 250 total points
ID: 24599321
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question