7.2 Day of Week Implementations

The following algorithms give the day of the week number (Sunday = 0) given a Gregorian Date.
  1. The following QBASIC implementation as a subroutine is valid for all Gregorian Dates up to the word limits of the implementation.

    SUB week1 (dow AS INTEGER, y1 AS LONG, m1 AS INTEGER, d1 AS INTEGER)
    m% = m1
    y& = y1
    IF m% < 3 THEN
          y& = y& - 1
          m% = m% + 12
    END IF
    dow = (d1 + (153 * m% - 457) \ 5 + INT(365.25# * y&) - INT(y& * .01) + INT(y& * .0025) + 2) MOD 7
    IF dow<0 THEN
          dow=dow+7
    END IF
    END SUB

  2. The following QBASIC implementation as a subroutine is valid for all Gregorian Dates from March 1 of the year 0 or greater. It uses integer arithmetic

    SUB week2 (dow AS INTEGER, y1 AS LONG, m1 AS INTEGER, d1 AS INTEGER)
    m% = m1
    y& = y1
    IF m% < 3 THEN
          y& = y& - 1
          m% = m% + 12
    END IF
    dow = (d1 + (153 * m% - 457) \ 5 + 365 * y& +y& \ 4 - y& \ 100 + y& \ 400 + 2) MOD 7
    END SUB

  3. A typical Visual Basic subroutine

    Sub JDtoWeek(jd As Double)
    'INPUT: jd = Julian Day Number
    'OUTPUT: modifies the caption of label dow
    i% = Int(jd + 1.5) Mod 7
    If i% < 0 Then i% = i% + 7
    dow.Caption = weekname(i%)
    End Sub


AESIR RESEARCH HOME PAGE

Last Modified: April 7, 2005