Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Tip/Trick

When Is Easter?

Rate me:
Please Sign up or sign in to vote.
4.94/5 (9 votes)
1 Feb 2017CPOL1 min read 11.9K   10   3
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.

C#
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).

C#
/// <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:

C#
DateTime gregorianEaster = ExtendDateTime.FindEasterSunday(2017);
DateTime julianEaster    = ExtendDateTime.FindEasterSunday(2017, ExtendDateTime.DateType.Julian);

If you want to find Good Friday:

C#
DateTime gregorianGoodFriday = ExtendDateTime.FindEasterSunday(2017).AddDays(-2);
DateTime julianGoodFriday    = ExtendDateTime.FindEasterSunday(2017, ExtendDateTime.DateType.Julian).AddDays(-2);

And of course, to find Easter Monday:

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
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.

Comments and Discussions

 
QuestionWhen is Easter Pin
Graham Irons2-Feb-17 20:04
Graham Irons2-Feb-17 20:04 
AnswerRe: When is Easter Pin
#realJSOP22-Sep-17 2:32
mve#realJSOP22-Sep-17 2:32 
SuggestionAdditional information on the algorithm Pin
Jochen Arndt2-Feb-17 1:19
professionalJochen Arndt2-Feb-17 1:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.