C#: Calculating Orthodox and Catholic Easter
Some time ago I was called about an online calendar for a company’s portal. The need was to inform employees about public holidays for the requested year. In Greece, some of those holidays are static meaning they occur the exact same date each year, but some are based on the Orthodox Easter so they move from date to date. There are a lot of online services that can return these information, but I thought I should give it a go and try writing the Orthodox Easter calculation in C#. Saying that, I found the algorithm online and since I was there I also found the Catholic Easter algorithm.
In this post you will find the calculation for the two Christianity events only, and in a later post I will also publish the public holidays calculation.
Have in mind that since the methods return
DateTime
“every possible year” is not quite accurate! And besides, both algorithms have problems after year 4200. You can read more about it here: http://en.wikipedia.org/wiki/Computus
This is the Orthodox Easter calculation:
/// <summary>
/// Get Orthodox easter for requested year
/// </summary>
/// <param name="year">Year of easter</param>
/// <returns>DateTime of Orthodox Easter</returns>
public static DateTime GetOrthodoxEaster(int year)
{
var a = year % 19;
var b = year % 7;
var c = year % 4;
var d = (19 * a + 16) % 30;
var e = (2 * c + 4 * b + 6 * d) % 7;
var f = (19 * a + 16) % 30;
var key = f + e + 3;
var month = (key > 30) ? 5 : 4;
var day = (key > 30) ? key - 30 : key;
return new DateTime(year, month, day);
}
And this is for the Catholic Easter:
/// <summary>
/// Get Catholic easter for requested year
/// </summary>
/// <param name="year">Year of easter</param>
/// <returns>DateTime of Catholic Easter</returns>
public static DateTime GetCatholicEaster(int year)
{
var month = 3;
var a = year % 19 + 1;
var b = year / 100 + 1;
var c = (3 * b) / 4 - 12;
var d = (8 * b + 5) / 25 - 5;
var e = (5 * year) / 4 - c - 10;
var f = (11 * a + 20 + d - c) % 30;
if (f == 24)
f++;
if ((f == 25) && (a > 11))
f++;
var g = 44 - f;
if (g < 21)
g = g + 30;
var day = (g + 7) - ((e + g) % 7);
if (day > 31)
{
day = day - 31;
month = 4;
}
return new DateTime(year, month, day);
}