Week day (Time calculation)

A value between 0 and 6.

Availability:

ECMAScript edition - 2
Property/method value type:Number primitive

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.

Example code:

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

Cross-references:

ECMA 262 edition 2 - section - 15.9.1.6

ECMA 262 edition 3 - section - 15.9.1.6