Calculate Easter Sunday (Python)

vegaseat 0 Tallied Votes 4K Views Share

Just in time, a short Python code snippet to calculate the date of Easter Sunday of a given year. This can be expanded to calculate the important date of Mardi Gras too.

# calculating the date of Easter Sunday
# tested with Python24    vegaseat    16apr2006

import datetime

def calc_easter(year):
    """returns the date of Easter Sunday of the given yyyy year"""
    y = year
    # golden year - 1
    g = y % 19
    # offset
    e = 0
    # century
    c = y/100
    # h is (23 - Epact) mod 30
    h = (c-c/4-(8*c+13)/25+19*g+15)%30
    # number of days from March 21 to Paschal Full Moon
    i = h-(h/28)*(1-(h/28)*(29/(h+1))*((21-g)/11))
    # weekday for Paschal Full Moon (0=Sunday)
    j = (y+y/4+i+2-c+c/4)%7
    # number of days from March 21 to Sunday on or before Paschal Full Moon
    # p can be from -6 to 28
    p = i-j+e
    d = 1+(p+27+(p+6)/40)%31
    m = 3+(p+26)/30
    return datetime.date(y,m,d)


# test the calc_easter function
if __name__ == '__main__':
    print calc_easter(2006)                       # 2006-04-16
    print calc_easter(2006).strftime("%d %B %Y")  # 16 April 2006
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.