Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I disable daylight savings in javascript

Posted on 2009-04-08
11
Medium Priority
?
1,215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

636 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