PDA

View Full Version : Calculating DST days


Tanker
10-28-2002, 09:54 PM
I've written a script that will calculate the correct sunday for start and end times for DST. It works, but after looking at it I think there is a better/shorter way to do it, i'm just not seeing it at the moment. I was hoping some of you gurus out there could take a look and maybe make some suggestions.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Date Check</title>
<script language="JavaScript1.2">
<!--
curDate = new Date();
curYear = curDate.getYear();
// Start at beginning of April and work forward to find the first sunday of the month.
myStartDate = new Date('April 1, '+curYear+' 1:59:59');
counter = myStartDate.getDay().toString();
switch (counter)
{
case "6" :
addDays=2
break;
case "5" :
addDays=3
break;
case "4" :
addDays=4
break;
case "3" :
addDays=5
break;
case "2" :
addDays=6
break;
case "1" :
var addDays=7
break;
case "0" :
var addDays=0
break;
default :
alert("IT BLOWED UP")
}
myStartDate = myStartDate.setDate(addDays);
dstOn = new Date(myStartDate);

// Start at end of october and work backwords to find the last sunday of the month.
myEndDate = new Date('October 31, '+curYear+' 1:59:59');
counter = myEndDate.getDay()
if(counter !=0){
for(i=0;i<counter;i++){
myEndDate = new Date(myEndDate - 24 * 60 * 60 * 1000);
DSTOFF_Date = new Date(myEndDate);
}
} else {
DSTOFF_Date = new Date(myEndDate);
}
dstOff = new Date(DSTOFF_Date)


function checkdate(){
if (curDate > dstOn && curDate < dstOff ) {
alert(dstOff + "\r\nPacific Daylight Savings Time");
} else {
alert(dstOn + "\r\nPacific Standard Time");
}
}
//-->
</script>
</head>

<body>
<script language="JavaScript1.2">
<!--
document.write("<table border=1>")
document.write("<tr><td>Today: </td><td>"+curDate+"</td></tr>")
document.write("<tr><td>DST Start: </td><td>"+dstOn+"</td></tr>")
document.write("<tr><td>DST End: </td><td>"+dstOff+"</td></tr>")
document.write("</table>")
//-->
</script>
<input type="button" value="Check Date" onClick="checkdate()">
</body>
</html>

beetle
10-28-2002, 10:28 PM
I'll be honest, I didn't look at the entire script, but your SWITCH statement is overkill. You can replace the whole thing with this.

var addDays = (counter==0) ? 0 : 8-parseInt(counter,10);

Get an optimization like this from everyone that reads this post and soon your code will only be 4 lines (hehe, j/k) :D

jalarie
10-29-2002, 07:54 PM
yr4=prompt('4-digit year',2002);
Dst=new Date(yr4,3,1);
DstB=Dst.getDay();
if (DstB == 0) { DstB=1; } else { DstB=8-DstB; }
DstBth='th';
if (DstB == 1) { DstBth='st'; };
if (DstB == 2) { DstBth='nd'; };
if (DstB == 3) { DstBth='rd'; };
Dst=new Date(yr4,9,1);
DstE=Dst.getDay();
if (DstE <= 4) { DstE=29-DstE; } else { DstE=36-DstE; }
DstEth='th';
if (DstE == 31) { DstEth='st'; };
Out ="Daylight Savings Time, where observed, begins on April ";
Out+=DstB+DstBth+" and ends on October "+DstE+DstEth+"."
alert(Out);

PauletteB
10-29-2002, 08:46 PM
Wouldn't work as a universal script as DST starts the last Sunday of October and ends the last Sunday of March in Australia, and likely in other countries in the southern hemisphere.

Tanker
10-30-2002, 04:13 AM
After looking into DST I realized that it wouldn't work for other areas, but since it's for a site that only lists pst/cst/est i'm not that worried about it.

If I decide to publish it I will adapt it to work for other territories. But for now it does what I need it to do. :)