When Is Easter?
Figuring out when Easter occurs in a given year is based on the phase of the moon, the vernal equinox, and where in the world you are.
Introduction
Easter is (I think) the only holiday that is based on something other than being a specific day in a month, or an nth day of week of a given month. In fact, Easter is based on the phase of the moon in relation to the occurrence of the vernal equinox.
The Code
The following method and should be placed in a public static class. Since I'm creating extension methods for the .Net DateTime struct, I call my class ExtendDateTime. The name of the class is unimportant, so you can call yours PrettySpringDress for all I care.
First things, first. I defined an enumerator so the programmer could specify which calendar to use.
public enum DateType { Gregorian, Julian };
The method below calculates when Easter Sunday occurs, and returns that date based on either the (default) gregorian calendar, or the julian calendar (many european countries observe the holiday based on the Julian calendar).
/// <summary>
/// Find the date on which Easter occurs.
/// </summary>
/// <param name="year">The year</param>
/// <param name="dateType">Whether you want the date based on the gregorian calendar (the default), or the Julian calendar.</param>
/// <returns>The calculated date</returns>
public static DateTime FindEasterSunday(int year, DateType dateType=DateType.Gregorian)
{
// I found this code somewhere on the internet, but I don't remember where.
int day = 0;
int month = 0;
int goldenNumber = year % 19;
int century = year / 100;
int daysToNextFullMoon = (century - (int)(century / 4) - (int)((8 * century + 13) / 25) + 19 * goldenNumber + 15) % 30;
int daysToEquinox = daysToNextFullMoon - (int)(daysToNextFullMoon / 28) * (1 - (int)(daysToNextFullMoon / 28) * (int)(29 / (daysToNextFullMoon + 1)) * (int)((21 - goldenNumber) / 11));
day = daysToEquinox - ((year + (int)(year / 4) + daysToEquinox + 2 - century + (int)(century / 4)) % 7) + 28;
month = 3;
if (day > 31)
{
month++;
day -= 31;
}
DateTime result = new DateTime(year, month, day);
// I added this code to convert the gregorian date to a julian calendar date. The
// code can easily be moved into an extension method if you need to perform this
// conversion for other reasons.
if (dateType == DateType.Julian)
{
// JulianCalendar is a .Net built-in class - who knew?!
JulianCalendar jCal = new JulianCalendar();
int jYear = jCal.GetYear(result);
int jMonth = jCal.GetMonth(result);
int jDay = jCal.GetDayOfMonth(result);
result = new DateTime(jYear, jMonth, jDay);
}
return result;
}
Usage
To find your preferred version of Easter, do one of the following:
DateTime gregorianEaster = ExtendDateTime.FindEasterSunday(2017);
DateTime julianEaster = ExtendDateTime.FindEasterSunday(2017, ExtendDateTime.DateType.Julian);
If you want to find Good Friday:
DateTime gregorianGoodFriday = ExtendDateTime.FindEasterSunday(2017).AddDays(-2);
DateTime julianGoodFriday = ExtendDateTime.FindEasterSunday(2017, ExtendDateTime.DateType.Julian).AddDays(-2);
And of course, to find Easter Monday:
DateTime gregorianEasterMonday = ExtendDateTime.FindEasterSunday(2017).AddDays(1);
DateTime julianEasterMonday = ExtendDateTime.FindEasterSunday(2017, ExtendDateTime.DateType.Julian).AddDays(1);
Points of Interest
There is no such thing as evolution. There is just a list of creatures Chuck Norris has allowed to live.
History
02 Feb 2017 - Original submission.