Calculating weekdays is a simple modulo and offset of the day number derived from the time value.
The formula for calculating day number is shown here:
t = an instant in time measured in milliseconds relative to 01-January-1970 UTC.
msPerDay = 86400000
Day(t) = floor(t/msPerDay)
WeekDay(t) = (Day(t) + 4) modulo 7
The resulting values will be from 0 to 6 with Sunday being represented by 0 and Saturday by 6.
By way of proof, WeekDay(0) should yield 4 which represents Thursday, 01-January-1970.
// Grab the time now in milliseconds myMilliseconds = new Date().getTime(); document.write("Day number ...: "); document.write(DayNumber(myMilliseconds)); document.write("<BR>"); document.write("Weekday number ...: "); document.write(WeekDayNumber(myMilliseconds)); document.write("<BR>"); // Work out day number from milliseconds function DayNumber(aMillisecondTime) { msPerDay = 86400000 myDay = Math.floor(aMillisecondTime/msPerDay); return myDay; } // Work out the week day number based on a Thursday start // This should be equivalent to Date.getDay(). function WeekDayNumber(aMillisecondTime) { return ((DayNumber(aMillisecondTime) + 4) % 7); }
See also: | Day number, Time range |
Prev | Home | Next |
WebTV | Up | Wheel() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |