The best way to make such a computation is to use the Julian date system.
Determining the Day of the Week from the Calendar Date:
Step 1: Determine the Julian Date (JD) of the calendar date. (The
Julian date is the number of days that have elapsed since noon,
January 1, 4713 B.C. Since it is primarily used by astronomers, it
begins at noon [so that the date doesn't change while they are looking
through their telescopes], and the Julian date of the beginning of a
day at midnight will always be a half-integer.)
Given the year Y, month M, and day D, if M = 1 or 2, then let
y = Y-1 and m = M+12 (this considers January and February as the
13th and 14th months of the previous year).& Otherwise, let y = Y and
m = M.
In the Gregorian calendar (after Oct. 15, 1582), let A = Int(y/100),
and B = 2 - A + Int(A/4).
In the Julian calendar (before Oct. 4, 1582), take B = 0
[Note: many countries didn't adopt the Julian calendar until much
later, so for a historical date, make sure you know which calendar was
being used.]
Then compute the Julian Date
& JD = Int(365.25*y)+Int(30.6001*(m+1))+D+B+1720994.5 .
Examples:
1. January 3, 1985.
Since M=1, we take y=1984 and m=13. &
& A = Int(1984/100)
= Int(19.84)
= 19
and
&
& B = 2 - 19 + Int(19/4)
= 2 - 19 + 4
= -13
Then
& JD= Int(365.25*1984)+Int(30.6001*14)+3-13+1720994.5
= Int(724656)+Int(428.4014)+3-13+1720994.5
= 724656 + 428 + 3 - 13 + 1720994.5
= 2446068.5
2. December 3 1993 is JD 2449324.5.
Step 2:& To find the day of the week, add 1.5 to the Julian date
calculated in step 1. Then find the REMAINDER W when (JD+1.5) is
divided by 7. For example, for January 3, 1985, when we divide 2446070
by 7, we get a quotient of& 349438 and a remainder of 4:
& JD + 1.5 = 7 * 349438 + 4
If the remainder is the day is
-------------------- & ------------
& 0 & Sunday
& 1 & Monday
& 2 & Tuesday
& 3 & Wednesday
& 4 & Thursday
& 5 & Friday
& 6 & Saturday
so January 3, 1985 was a Thursday.
December 3, 1993 was JD 2449324.5, and JD+1.5 divided by 7 leaves a
remainder of 5, so the day is a Friday.
You can use the following code:
program date;
const week: array[1..7] of string = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var d, m, y, jd, a, b:real; i:integer;
begin
writeln('Enter date in format Day Month Year:');
readln(d, m, y);
if (m=1) or (m=2) then
begin
y:=y-1;
m:=m+12;
end;
a:=y/100;
b:=(2-a)+(a/4);
jd:=(365.25*y)+(30.6001*(m+1))+(d+b)+1720994.5+1.5;
i:=round(trunc(((jd/7)-trunc(jd/7))*7));
writeln(week[i+1]);
end.
Comments
Leave a comment