Date calculation code
 MDFS::Info.Comp.Time.Date/htm Search  

Calculating the day of the week

The Gregorian Calendar runs on a 400-year cycle. Leap years are calculated with:
  • A leap year is a year wholly divided by 4
  • Unless it is wholly divided by 100, then it is not a leap year
  • Unless it is wholly divided by 400, then it is a leap year
    So: 1600, 2000, 2400 are leap years, but 1900, 2100 etc are not.

    Over 400 years there are 97 leap days, so the day of the week advances 497 times, 497/7 gives a remainder of 0, so over the 400-year Gregorian cycle the day of the week resets to the same day. This means that the day of the week can be calculated by reducing the year to a 0-399 offset within any 400-year cycle.

    The standard modification of Zeller's formula to calculate the day of the week is:
      offset[]={7,3,2,5,0,3,5,1,4,6,2,4}
      if (month < 3) then year=year-1
      day=(year + year/4 - year/100 + year/400 + offset[month] + day) MOD 7

  • 8-bit code, year 1900-2155

  • 6502:
  • Z80:
  • PDP11:
  • ARM:
  • DayOfWeek 
    DayOfWeek
    DayOfWeek
    DayOfWeek
    DofWeek
    DofWeek
    DofWeek
    DofWeek

    16-bit code, any year 0-65535

  • 6502:
  • 6502:
  • BBC BASIC:




  • DayOfWk16a 
    DayOfWk16b 
    BLib/Date.txt
    DofWeek16a
    DofWeek16b
    BLib/Date

    Incrementing a calendar date

    This code will increment a date, correctly rolling over from the last day of a month to the first day of the next month, or the next year.
  • 6502:
  • DateUpdate

    Optimised *60 and *100

    When manipulating clock times, you often need to multiply by 60 and 100, for example centiseconds=((hours*60+minutes)*60+seconds)*100.

    This is a small snippet of optimised code to do multiplication by 60 or 100. There are 360000 = &057E40 centiseconds in a day, so this fits into a 3-byte 24-bit number.

  • 6502:


  • Times60

    Divide into hrs/min/sec/cs

    This is simple unoptimised code to divide a centisecond count into hours, minutes, seconds, and centiseconds; the reverse of Times60.
  • 6502:
  • Divide60

    Best viewed with Any Browser Valid HTML 4.0! Authored by J.G.Harston
    Last update: 12-Mar-2023