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