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).
public static DateTime FindEasterSunday(int year, DateType dateType=DateType.Gregorian)
{
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);
if (dateType == DateType.Julian)
{
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.
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.