Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Article

Calculating Christian Holidays

Rate me:
Please Sign up or sign in to vote.
4.11/5 (22 votes)
18 Jul 2005CPOL2 min read 120.9K   988   36   21
Calculate dates for christian holidays such as Easter, Ascension day etc.

Introduction

I was looking for a way to calculate the date for Easter Sunday when I found Oskar Wieland's article, Calculating Easter Sunday, here at Code Project. It describes a method in C++ and I needed the code in C#. I then realized that Easter is not the only Christian holiday that can be calculated. Some are directly related to Easter, others are related to Christmas. I therefore created a simple C# class that will return dates for the following Christian Holidays:

  • Easter Sunday
  • Good Friday (Friday before Easter Sunday)
  • Palm Sunday (1 week before Easter Sunday)
  • Whit Sunday (7 weeks after Easter Sunday)
  • Ascension Day (10 days before Whit Sunday)
  • Ash Wednesday (47 days before Easter)
  • First Sunday of Advent (The Sunday between November 26 and December 3)

The holidays calculated by this class are the major Christian holidays. All others can be calculated using one of these methods, or have a fixed date in the year.

Oskar Wieland's algorithm in C#

I converted the original C++ code to C# first. But I was pointed to the fact that sometimes the routine returns incorrect values. See the response by Sire404 below. I updated the code using that response. The algorithm to calculate Easter Sunday is shown here:

C#
public static void EasterSunday(int year, ref int month, ref int day)
{
    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;
    }
}

The next step was creating an overloaded version of this method which returns a DateTime value. That method forms the basis for all Easter related date methods:

C#
public static DateTime EasterSunday(int year)
{
    int month = 0;
    int day = 0;
    EasterSunday(year, out month, out day);

    return new DateTime(year, month, day);
}

Calculating other dates

Now that the code to calculate Easter Sunday is ready, creating calculations for other dates based on Easter Sunday is very simple. All we need to know is how these dates are related to Easter. Below you will find the code that will calculate the dates for Ascension Day and Whit Sunday.

Ascension Day

Ascension Day is 10 days before Whit Sunday, or 39 days after Easter.

C#
public static DateTime AscensionDay(int year)
{
    return EasterSunday(year).AddDays(39);
}

Whit Sunday

Whit Sunday is 7 weeks after Easter Sunday.

C#
public static DateTime WhitSunday(int year)
{
    return EasterSunday(year).AddDays(49);
}

For the other calculations please refer to the source code which you can download.

Calculating first Sunday of Advent

The first Sunday of Advent, is related to Christmas. The first Sunday of Advent is the 4th Sunday before Christmas, between November 26 and December 3. So to calculate that date, I use the following algorithm:

C#
public static DateTime FirstSundayOfAdvent(int year)
{
    int weeks = 4;
    int correction = 0;
    DateTime christmas = new DateTime(year, 12, 25);

    if (christmas.DayOfWeek != DayOfWeek.Sunday)
    {
        weeks--;
        correction = ((int)christmas.DayOfWeek - (int)DayOfWeek.Sunday);
    }
    return christmas.AddDays(-1 * ((weeks * 7) + correction));
}

Using the code

The code should work fine in any version of Visual Studio .NET, but the demo application was written in Visual Studio 2003. All methods are documented using XML-style comments making it easy to integrate it into your applications or class libraries.

Credits

  • Oskar Wieland for posting the original C++ code which got me started.
  • Jean Meeus for providing me this link to Wikipedia for more information on how Easter and other Christian Holidays can be calculated.
  • Sire404 for pointing out the bug in my EasterSunday method.

License

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


Written By
Technical Lead
Netherlands Netherlands
I'm a professional software developer for a small company in the Netherlands. I have been developing software since 1988 in C, Visual Basic and C#. My main focus is now on problem analysis in IT projects.

Comments and Discussions

 
QuestionThese method's on Pin
Henrique Clausing4-Dec-19 6:24
Henrique Clausing4-Dec-19 6:24 
QuestionUpdated link to Oskar Wieland article Pin
User 505364330-Jun-18 3:59
User 505364330-Jun-18 3:59 
Questionand the laziest version to calc easternsunday in c# (thx to gaus) Pin
caosFrank12-Jan-17 22:24
caosFrank12-Jan-17 22:24 
QuestionIncomplete article -- Calculation for non-Catholic ("Eastern") Easter Pin
dc_20003-Sep-15 12:30
dc_20003-Sep-15 12:30 
AnswerRe: Incomplete article -- Calculation for non-Catholic ("Eastern") Easter Pin
Jan Schreuder19-Jan-16 21:12
Jan Schreuder19-Jan-16 21:12 
GeneralRe: Incomplete article -- Calculation for non-Catholic ("Eastern") Easter Pin
dc_200026-Jan-16 14:33
dc_200026-Jan-16 14:33 
NewsComputus in scala and java Pin
mike mainguy23-Apr-11 3:48
mike mainguy23-Apr-11 3:48 
GeneralGood article Pin
Donsw21-Dec-08 10:38
Donsw21-Dec-08 10:38 
GeneralRe: Good article Pin
Donsw21-Dec-08 10:42
Donsw21-Dec-08 10:42 
GeneralNot all Christians observe the same Easter period Pin
Gerard Nicol19-Jul-05 12:12
Gerard Nicol19-Jul-05 12:12 
GeneralRe: Not all Christians observe the same Easter period Pin
Jan Schreuder19-Jul-05 19:45
Jan Schreuder19-Jul-05 19:45 
GeneralInteresting article Pin
Judah Gabriel Himango18-Jul-05 9:03
sponsorJudah Gabriel Himango18-Jul-05 9:03 
GeneralThe algorithm is incorrect Pin
Sire40413-Jul-05 21:56
Sire40413-Jul-05 21:56 
GeneralRe: The algorithm is incorrect Pin
Borrisholt5-Mar-08 2:51
Borrisholt5-Mar-08 2:51 
GeneralRe: The algorithm is incorrect Pin
Sire4045-Mar-08 3:11
Sire4045-Mar-08 3:11 
GeneralRe: The algorithm is incorrect Pin
Jan Schreuder5-Mar-08 9:01
Jan Schreuder5-Mar-08 9:01 
GeneralRe: The algorithm is incorrect Pin
Herman<T>.Instance24-May-11 9:05
Herman<T>.Instance24-May-11 9:05 
GeneralJean Meeus Pin
Keith Farmer1-Jul-05 8:34
Keith Farmer1-Jul-05 8:34 
GeneralRe: Jean Meeus Pin
Keith Farmer1-Jul-05 8:36
Keith Farmer1-Jul-05 8:36 
GeneralRe: Jean Meeus Pin
Jan Schreuder2-Jul-05 9:36
Jan Schreuder2-Jul-05 9:36 
GeneralRe: Jean Meeus Pin
Keith Farmer2-Jul-05 12:32
Keith Farmer2-Jul-05 12:32 

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.