[comp.lang.pascal] Day of the week

db@tc.fluke.COM (Dan Banay) (04/12/90)

The following function will return the day of the week for a given date:

(* e.g. m = 8 d = 10 y = 1988 *)
(* returns 0 = Sunday... *)
(* uses Zeller's Algorithm *)
function dayw(m,d,y:integer):integer;
var century:integer;
begin

  if (m > 2)
    then
        m := m -2
  else
        begin
          m := m + 10;
          y := y - 1
        end;

  century := y div 100;
  y := y mod 100;

  dayw := (d-1+((13*m-1) div 5)+(5*y div 4)+(century div 4)-2*century+1) mod 7;
end;


Examples:

  x := dayw(8,10,1988); (* 3  Wed *)
  y := dayw(2,29,1992); (* 6  Sat *)

I can't remeber where I found the algorithm (from a magazine or book) since
I'm getting old and senile. ;-)   If anyone has additional information about
Zeller's algorithm, I'd be interested in hearing about it.

Hope this helps,
Dan