Function to return date of Easter for the given year

前端 未结 8 1437
眼角桃花
眼角桃花 2020-12-08 05:12

So, here\'s a funny little programming challenge. I was writing a quick method to determine all the market holidays for a particular year, and then I started reading about E

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 05:27

    Python: using dateutil's easter() function.

    >>> from dateutil.easter import *
    >>> print easter(2010)
    2010-04-04
    >>> print easter(2011)
    2011-04-24
    

    The functions gets, as an argument, the type of calculation you like:

    EASTER_JULIAN   = 1
    EASTER_ORTHODOX = 2
    EASTER_WESTERN  = 3
    

    You can pick the one relevant to the US.

    Reducing two days from the result would give you Good Friday:

    >>> from datetime import timedelta
    >>> d = timedelta(days=-2)
    >>> easter(2011)
    datetime.date(2011, 4, 24)
    >>> easter(2011)+d
    datetime.date(2011, 4, 22)
    

    Oddly enough, someone was iterating this, and published the results in Wikipedia's article about the algorithm:

    alt text

提交回复
热议问题