Calendar Math

Calendar Math is a special interest of mine. 
 
It has always fascinated me how three numbers (day, month year), can be plugged into a mathematical formula, resullt in a number...which represents the number of days that have elapsed since 1 January 4713 B.C.

Some years ago I began collecting the math.

Julian Day Number Formulae
    d=day
    m=month
    y = year
    IP = integer part

I
If m < 3:
    y = y - 1, m = m + 12
If date is later than 15821014:
    a = IP(y / 100)
    b = 2-a + IP(a / 4)
else b = 0
if y < 0:
    c = IP((365.25 * y) - .75)
else c = IP(365.25 * y)
f = IP(30.6001 * (m + 1))
Julian Day Number (midnight, GMT) = b + c + f + d + 1720994.5

II
a = IP((14 - m) / 12)
y = y + 4800 - a
m = m + (12 * a) - 3

For Gregorian dates (15831015 and after):
j = d + IP(((153 * m + 2) / 5) + (365 * y) + (IP(y / 4) - IP(y / 100) + IP(y / 400) - 32045

For Julian dates:
j = d + IP(((153 * m + 2) / 5)  + (365 * y) + (IP(y / 4) - 32083

This result is an integer, the JDN at noon, gmt.

III
Gregorian Dates:
If m < 3:
    y = y - 1, m = m + 12
j = (2 - (IP(y / 100)) + IP((IP(y / 100) / 4))) + (IP(365.25 * y)) + (IP(30.6001 * (m + 1))) + d + 1720994.5

IV
Using the  ordinal day number (see equation(s) below):
y = y + 4799
 j = o + y * 365 + IP(y / 4) - IP(y / 100) + IP(y/400) - 31739

note: julian day numbers are used in astronomy, and thus they begin at noon, gmt; 2434239.5 represents 15 aug 1952, 0000 hrs, gmt
2434240 = 15 aug 1952 1200 hrs gmt.

Julian Day -> Date
I
i = IP(j + .5)
f = FP(j + .5)
If i > 2299160
    a = IP(i - 1867216.23) / 36524.25)
    b = i + 1 + a - IP(a / 4)
else b=1
c = b + 1524
h = IP(((c - 122,1) / 365.25))
e = IP(365.25* h)
g = IP(((c-e) / 30.6001))

d = c - e  + f  - IP(30.6001  * g)
This is the date, including the fractional part of the day.
If g < 13.5
     m = g - 1
else m = g -13
This is the month.
If m > 2.5
    y = h - 4716
else y = h - 4715
This is the year.

II
For the Gregorian calendar:
a = j + 32044
b = IP(((4 * a) + 3) / 146096)
c = a - IP((146097 * b) / 4)
For the Julian calendar:
b = 0
c = j + 32082
For both calendars:
d = IP(((4 * c) + 3) / 1461)
e = c - IP((1461*d) / 4)
m = IP(((5 * e) + 2) / 153)

day = c - IP(((153 * m) + 2) / 5) + 1
month = m + 3 - 12 * IP(m / 10)
year = (100 * b) + d - 4800 +  IP(m / 10)

Other Day Counts

The Modified Julian Day begins at midnight, gmt and is calculated as Julian Day -  2400000.5

Rata Die

Rata Die is the number of days since 1 Jan 1, in the Gregorian calendar:
If m < 3
    y = y - 1, m = m + 12 
R = d + IP((153 * m - 457) / 15) + 365 * y + IP(y / 4) - IP(y / 100) + IP(y / 400) - 306
(Rata Die = Julian Day - 1721425)

Ordinal Day

The number of days since 1 Jan:
I
 o = IP(275 * m / 9) - (IP((m + 9) / 12)) * (1 + IP((y - 4 * IP(y / 4) + 2) / 3)) + d - 30
II
y = year + 4799
o =  j -  (y  * 365 +  IP(y / 4) - IP(y / 100) + IP(y/400) - 31739

Week Number:

I
Using o from the above equation
week# = IP((o -  5 + 10) /  7)

II
a = (j + 31741 - (j mod 7)) mod 146097 mod 36524 mod 1461
b = IP(a / 1460)
c = ((a - b) mod 365) + b
week# = IP(c / 7) + 1
Mod refers to modular division, i.e., returns the remainder of the division

Day of the Week
Where 0 = Sunday, 1- 6 = Monday - Saturday:
(j mod 7) + 1
or simply
rata die mod 7