That is the question. Rules in North America * for changing the time:
- the first Sunday of November , the shift of changes to the standard (-1 hour)
- second Sunday of March , daylight shift (your normal shift from GMT)
Consider a function in JavaScript that takes a Date parameter and needs to determine if the argument is standard or summer.
The root of the question:
- How would you plot the date for the next time change?
Now the algorithm / pseudo-code is as follows:
if argDate == "March"
{
var firstOfMonth = new Date ();
firstOfMonth.setFullYear (year, 3.1);
// the day of week (0 = Sunday, 6 = Saturday)
var firstOfMonthDayOfWeek = firstOfMonth.getDay ();
var firstSunday;
if (firstOfMonthDayOfWeek! = 0) // Sunday!
{
// need to find a way to determine which date is the second Sunday
}
}
The limitation here is to use the standard JavaScript function, and not to clear any parsing of the JavaScript Date object. This code will not run in the browser, so these great solutions will not apply.
** Not all places / regions in North America change time. *
javascript algorithm datetime
p.campbell
source share