Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

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);
}

Comments

TrackBack said:

# July 1, 2005 3:51 AM

TrackBack said:

# July 1, 2005 3:55 AM

jack said:

Thanks, great code

# July 11, 2007 6:04 PM

Tom said:

thanks for sharing!

# July 20, 2009 11:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 3 and 8 and type the answer here: