Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsDaylightSavingTime

Status
Not open for further replies.

slatet

Programmer
Sep 11, 2003
116
0
0
US
Is anyone good at using IsDaylightSavingTime? I grabbed clock code off of a website but I have to change it for daylight savings. I found IsDaylightSavingTime might be the solution but am struggling to get it to work. Here is the code:

var day="";
var month="";
var myweekday="";
var year="";
newdate = new Date();
mydate = new Date();
var myzone = newdate.getTimezoneOffset();
newtime=newdate.getTime();

var zone = 6; // references your time zone

if (myzone.IsDaylightSavingTime(Now)) { zonea = zone - 1 ;
dst = " MDST";
}
else {
zonea = zone ; dst = " MST";
}

....theres more of course...

Thanks in advance.
 
Daylight Savings Time is a vexed issue to compute for the world:

- different countries in the same hemisphere start/end a summer offset on different days ...

- summer in the Southern hemisphere is winter in the Northern hemisphere

- confusion abounds since the globe usually has 2 dates simultaneously - on boundary days any one can be in or out of daylight savings

- some areas are on double daylight (or soon will switch to it)

You do not need to worry about the above ... but bear it in mind before you try some exotic "world global time zone" programming.

IGNORING the issues above... The following Javascript code gives some idea of what can be done to detect daylight Savings PC's versus those not on Daylighht Savings.




<html>

<p>

<script language="javascript">

var ww = new Date();
var xx = new Date();
var yy = new Date();

var tt = ww.getTimezoneOffset();

var gg = xx.getUTCHours();
var mm = xx.getUTCMinutes();

var ll = yy.toLocaleString() ;


var aa = xx.getHours();
var bb = xx.getMinutes();

var actualdiff = (gg-aa)*60+(mm-bb);


document.write('<p>');
document.write('Time Zone Offset(mins.) ='+tt+' UTC='+gg+':'+mm);
document.write('<p>');
document.write('Actual Offset Diff(mins.)='+actualdiff+' Local='+aa+':'+bb);


var insyncwithGMT = 0 ;
if (actualdiff==tt)
{
document.write('<p>Same Offset Means Same Daylight Shift as British GMT');
insyncwithGMT = 1 ;
}


var dtoday = new Date() ;
var dnweekday = dtoday.getDay() ; // day of week 0..6
var dnmonth = dtoday.getMonth() ; // month of year 0..11
var dndaymon = dtoday.getDate() ; // day of month 0..31
var dnyear = dtoday.getFullYear() ; // year e.g. 2009

var tdiff=999;

var dwkdate = new Date() ;


var springChangeover = new Date() ;

springChangeover.setFullYear(dnyear,2,21) ; // when is march 21
var dnweekday = springChangeover.getDay() ; // day of week 0..6 for march 21

if(dnweekday==0)
{
}
else
{
var tdiff = 7-dnweekday ;
springChangeover.setDate(springChangeover.getDate()+tdiff)
}




var fallChangeover = new Date() ;

fallChangeover.setFullYear(dnyear,9,23) ; // when is october 23
var dnweekday = fallChangeover.getDay() ; // day of week 0..6 for october 23
if(dnweekday==0)
{
}
else
{
var tdiff = 7-dnweekday ;
fallChangeover.setDate(fallChangeover.getDate()+tdiff)
}


var DTmsg = 'British Summer Time from ' +(''+springChangeover).substring(0,10) + ' to ' +(''+fallChangeover).substring(0,10)+ ' - ' ;

//
// are we in summer in the N. hemisphere
//

var inNHSummer = 1 ;

if (dtoday>=fallChangeover)
{
inNHSummer= 0 ;
}

else {
if (dtoday<springChangeover)

{ inNHSummer= 0 ; }

}


document.write('<p>'+DTmsg) ;

if (inNHSummer==1) { document.write('<p>British Summer Time is In Effect Now') ; }


if( (inNHSummer==1)&&(insyncwithGMT = 1) )

{ document.write('<p>Local machine is in Daylight Savings Time') ; }

</script>



 
Sorry - I had to correct TWO ERRORS

(1) Correct my typo at bottom of script.

(2) Recognize 1440 as zero minutes difference

<html>

<p>

<script language="javascript">

var ww = new Date();
var xx = new Date();
var yy = new Date();

var tt = ww.getTimezoneOffset();

var gg = xx.getUTCHours();
var mm = xx.getUTCMinutes();

var ll = yy.toLocaleString() ;


var aa = xx.getHours();
var bb = xx.getMinutes();

var actualdiff = (gg-aa)*60+(mm-bb);


document.write('<p>');
document.write('Time Zone Offset(mins.) ='+tt+' UTC='+gg+':'+mm);
document.write('<p>');
document.write('Actual Offset Diff(mins.)='+actualdiff+' Local='+aa+':'+bb);


var insyncwithGMT = 0 ;

var calcdiff = actualdiff-tt ;

if ((calcdiff==1440)||(calcdiff==-1440))
{ calcdiff=0; }

if (calcdiff==0)
{
document.write('<p>Same Offset Means Same Daylight Shift as British GMT');
insyncwithGMT = 1 ;
}
else
{
document.write('<p>'+calcdiff+' Minutes Offset Means NOT Same Daylight Shift as British GMT');
insyncwithGMT = 0 ;
}




var dtoday = new Date() ;
var dnweekday = dtoday.getDay() ; // day of week 0..6
var dnmonth = dtoday.getMonth() ; // month of year 0..11
var dndaymon = dtoday.getDate() ; // day of month 0..31
var dnyear = dtoday.getFullYear() ; // year e.g. 2009

var tdiff=999;

var dwkdate = new Date() ;


var springChangeover = new Date() ;

springChangeover.setFullYear(dnyear,2,21) ; // when is march 21
var dnweekday = springChangeover.getDay() ; // day of week 0..6 for march 21

if(dnweekday==0)
{
}
else
{
var tdiff = 7-dnweekday ;
springChangeover.setDate(springChangeover.getDate()+tdiff)
}




var fallChangeover = new Date() ;

fallChangeover.setFullYear(dnyear,9,23) ; // when is october 23
var dnweekday = fallChangeover.getDay() ; // day of week 0..6 for october 23
if(dnweekday==0)
{
}
else
{
var tdiff = 7-dnweekday ;
fallChangeover.setDate(fallChangeover.getDate()+tdiff)
}


var DTmsg = 'British Summer Time from ' +(''+springChangeover).substring(0,10) + ' to ' +(''+fallChangeover).substring(0,10)+ ' - ' ;

//
// are we in summer in the N. hemisphere
//

var inNHSummer = 1 ;

if (dtoday>=fallChangeover)
{
inNHSummer= 0 ;
}

else {
if (dtoday<springChangeover)

{ inNHSummer= 0 ; }

}


document.write('<p>'+DTmsg) ;

if (inNHSummer==1) { document.write('<p>British Summer Time is In Effect Now') ; }


if( (inNHSummer==1)&&(insyncwithGMT == 1) )

{ document.write('<p>Local machine is in Daylight Savings Time') ; }

</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top