转载

判断是否是闰年

判断是否为闰年的方法很多,如:

指定日期,判断其所属年份是否为闰年https://www.cnblogs.com/insus/p/10841868.html
C#或VB.NET判断指定的年份是否为闰年https://www.cnblogs.com/insus/archive/2012/06/05/2537446.html

其实,我们还有一个方法,就是知道了2月份的天数,或是最后一天是28号还是29号,从而也知道这一年是平年还是闰年。

因为平年28,闰年29.

T-SQL获取二月份天数https://www.cnblogs.com/insus/archive/2011/04/22/2025019.html

如何获取月份的天数https://www.cnblogs.com/insus/archive/2011/09/10/2173028.html

获取某一日期所在月份的第一天日期或最后一天日期https://www.cnblogs.com/insus/p/10841647.html

某一日期所在月份的天数https://www.cnblogs.com/insus/p/10841741.html

获取某一日期所在月份的第一天日期或最后一天日期https://www.cnblogs.com/insus/p/10841647.html


 

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      Insus.NET
-- Create date: 2019-05-14
-- Update date: 2019-05-14
-- Description: 判断是否为闰年
-- =============================================

CREATE FUNCTION [dbo].[svf_IsLeapYear] ( 
    @Year INT
)
RETURNS BIT
AS
BEGIN
    DECLARE @rtv BIT = 0,
            @February_1st DATETIME = CAST((CAST(@Year AS NVARCHAR(4)) + N'-02-01') AS DATETIME)
    
    --IF DAY([dbo].[svf_LastDayOfTheMonth](@February_1st)) = 29
    IF DAY(DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,@February_1st) + 1,0))) = 29

        SET @rtv = 1
    RETURN @rtv
END
GO
Source Code

 

 

正文到此结束
本文目录