Archived

This topic is now archived and is closed to further replies.

AndreTheGiant

simple date calculation

Recommended Posts

Given a year (eg 2005) and a month (eg May) how can I calculate - the number of days in that month (accounting for leap years too) - the day that is the first of that month (eg Wednesday) ? Apologies if this has been asked 17603 times.

Share this post


Link to post
Share on other sites
Determining if the year is a leap year or not is not difficult. If the year is divisible by four, except for 00 years, unless the 00 year is divisible by 400, then the year is a leap year. What could be simpler?

Calculating the number of days in a month is easy too. Just keep a list of how many days are in each month for a normal year. If a leap year, add one to every month''s result except for January''s.

As for what day is the first day of the month, I don''t know off hand. I''ll have to think about it. There are some programs that do that (see the UNIX''s cal command).

Share this post


Link to post
Share on other sites
Maybe this can help you:


program Zeller;

uses Crt;

const
start_year=1990;
end_year=2001;

{ days : array[1..7] of string= (''Sunday'', ''Monday'', ''Tuesday'', ''Wednesday'', ''Thursday'', ''Friday'', ''Saturday'');}
var
d, m, y : integer; {for cycles}

{Zeller function: returns the day of the week
for this date, month and year-
[0;6]; 0 is Saturday}

function return_day(day, month, year : integer) : byte;
var
temp, yr1, yr2 : integer;
Begin
if month<3 then
Begin
inc(month,10);
dec(year);
End
else dec(month,2);

yr1:=year div 100; yr2:=year mod 100;
temp:=(26*month-1) div 10;

return_day:=(day+temp+yr2+(yr2 div 4)+(yr1 div 4)-2*yr1+49) mod 7;
End;

Begin
ClrScr;

{ writeln(''Today is '' ,days[return_day(14,12,2001)+1]);}

writeln(''Period from '', start_year, '' to '', end_year, '':'');
for y:=start_year to end_year do
for m:=1 to 12 do
if return_day(13,m,y)=5 then
writeln(''Friday 13 was: in '', m, '' month of the '', y, '' year'', '' ('', 13, ''.'', m, ''.'', y, '').'');
End.
[\source]

Share this post


Link to post
Share on other sites