Function to return date of Easter for the given year

前端 未结 8 1419
眼角桃花
眼角桃花 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:49

    The SQL Server function below is more general than the accepted answer

    The accepted answer is only correct for the range (inclusive) : 1900-04-15 to 2099-04-12

    It uses the algorithm provided by The United States Naval Observatory (USNO)

    http://aa.usno.navy.mil/faq/docs/easter.php

    CREATE FUNCTION dbo.GetEasterSunday (@Y INT)
    RETURNS DATETIME
    AS
        BEGIN 
    
            -- Source of algorithm : http://aa.usno.navy.mil/faq/docs/easter.php
    
            DECLARE @c INT = @Y / 100
            DECLARE @n INT = @Y - 19 * (@Y / 19)
            DECLARE @k INT = (@c - 17) / 25
            DECLARE @i INT = @c - @c / 4 - (@c - @k) / 3 + 19 * @n + 15
            SET @i = @i - 30 * (@i / 30)
            SET @i = @i - (@i / 28) * (1 - (@i / 28) * (29 / (@i + 1)) * ((21 - @n) / 11))
            DECLARE @j INT = @Y + @Y / 4 + @i + 2 - @c + @c / 4
            SET @j = @j - 7 * (@j / 7)
            DECLARE @l INT = @i - @j
            DECLARE @m INT = 3 + (@l + 40) / 44
            DECLARE @d INT = @l + 28 - 31 * (@m / 4)
    
            RETURN 
        ( 
            SELECT CONVERT 
            (  DATETIME, 
                     RTRIM(@Y)  
                + RIGHT('0'+RTRIM(@m), 2)  
                + RIGHT('0'+RTRIM(@d), 2)  
        ) 
        )
        END 
    
    
    GO
    

提交回复
热议问题