How to: Calculate easter sunday
We needed to know when Easter Sunday was in a given year for my current project. I found C++ code to do this at Code Project and converted it to a C#. I then reposted the code on Code Project. But someone found a bug in the original algorithm and posted an improvement as a response to my code. The code below is the modified result, which produces accurate results:
public static void EasterSunday(int year, ref int month, ref int day)
{ if (year < 1 || year > 9999) throw new ArgumentOutOfRangeException("year", year, "Must be between 0001 and 9999.");
int g = year % 19;
int c = year / 100;
int h = h = (c - (int)(c / 4) - (int)((8 * c + 13) / 25) + 19 * g + 15) % 30;
int i = h - (int)(h / 28) * (1 - (int)(h / 28) * (int)(29 / (h + 1)) * (int)((21 - g) / 11));
day = i - ((year + (int)(year / 4) + i + 2 - c + (int)(c / 4)) % 7) + 28;
month = 3;
if (day > 31)
{ month++;
day -= 31;
}
}
And from there, it is only a few steps to the following very useful methods:
public static DateTime EasterSunday(int year)
{ if (year < 1 || year > 9999) throw new ArgumentOutOfRangeException("year", year, "Must be between 0001 and 9999.");
int month = 0;
int day = 0;
EasterSunday(year, ref month, ref day);
return new DateTime(year, month, day);
}
public static DateTime AscensionDay(int year)
{ if (year < 1 || year > 9999) throw new ArgumentOutOfRangeException("year", year, "Must be between 0001 and 9999.");
return EasterSunday(year).AddDays(39);
}
public static DateTime WhitSunday(int year)
{ if (year < 1 || year > 9999) throw new ArgumentOutOfRangeException("year", year, "Must be between 0001 and 9999.");
return EasterSunday(year).AddDays(49);
}