Solved

How do I disable daylight savings in javascript

Posted on 2009-04-08
11
1,077 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now