The hebrew calendar is a lunar calendar. Before I get into the calculation, let me try to explain lunar calendars. Each month goes from new moon to new moon. Between moladot (new moons) is (according to hebrew calendar) 29 days, 12 hours (abreviated h) and 793 (of 1080) chelekim (parts abreviated p). If one knows one new moon, they could find any other new moon by adding or subtracting this interval. It also happens that every 19 solar years corresponds to exactly 235 lunar months. This means you can devise a 19 lunar year cycle made up of 12 years of 12 lunar months and 7 years of 13 lunar months that corresponds to an equivalent 19 solar years. The hebrew calendar has 13 month (leap) years in the 3rd, 6th, 8th, 11th, 14th, 17th and 19th years of this cycle. In the hebrew calendar the leap month is done by adding a second Adar of 30 days.
Now if the Hebrew calendar was strickly based on this, we could easily calculate one Rosh Hashana to the next, and the months would alternate 29 and 30 days. Things would be easy, but this is not the case. First the extra 793p have to be balenced off. Also Rosh Hashana (RH) must be moved to prevent certain calendar facts from happening (like Yom Kippur from landing on Friday or Sunday). RH is moved by the following 4 rules (remember - hebrew day starts at sunset) :
These reasons mean a year can have 353, 354, 355 days in non-leap-years, and 383, 384 and 385 in leap-years. To balence this off, in short years (353 and 383 days) Kislev is shortened to 29 days and in long years (355 and 385 days) Heshvan is lengthened to 30 days.
Now before things get really hopeless, there is a simple method here.
|Year length||leap year||Heshvan length||Kislev length|
The intercalculation of the Gregorian and the Hebrew date is not that complex. The trick is not to calculate one from the other, but to set some base date to calculate from. To convert one to the other you first calculate the number of days from the base date, and then calculate the other from that number of days. In my recent version of the software the base date is January 1, 1760 = day 1. The next 19 year cycle molad occurs at Rosh Hashana 5530 September 30, 1769 = day 3560 at 21h 549p. This date is not chosen at random. Before 1763, the Gregorian calendar is not standard and we must start at the begining of the next 19 year lunar cycle.
The follow is psuedo code for this operation, some basic definitiona, the four basic routines, and some helper routines