This article is from the Calendars FAQ, by Claus Tondering claus@tondering.dk with numerous contributions by others.
Try this one (the divisions are integer divisions, in which remainders
are discarded):
a = (14-month)/12
y = year+4800-a
m = month + 12*a - 3
For a date in the Gregorian calendar:
JD = day + (153*m+2)/5 + y*365 + y/4 - y/100 + y/400 - 32045
For a date in the Julian calendar:
JD = day + (153*m+2)/5 + y*365 + y/4 - 32083
JD is the Julian day number that starts at noon UTC on the specified
date.
The algorithm works fine for AD dates. If you want to use it for BC
dates, you must first convert the BC year to a negative year (e.g.,
10 BC = -9). The algorithm works correctly for all dates after 4800 BC,
i.e. at least for all positive Julian day numbers.
To convert the other way (i.e., to convert a Julian day number, JD,
to a day, month, and year) these formulas can be used (again, the
divisions are integer divisions):
For the Gregorian calendar: a = JD + 32044 b = (4*a+3)/146097 c = a - (b*146097)/4 For the Julian calendar: b = 0 c = JD + 32082 Then, for both calendars: d = (4*c+3)/1461 e = c - (1461*d)/4 m = (5*e+2)/153 day = e - (153*m+2)/5 + 1 month = m + 3 - 12*(m/10) year = b*100 + d - 4800 + m/10
 
Continue to: