在要求输入邮箱的文本域,请填写真实的邮件地址。非真实邮件地址,将收不到回复信息。

C#判断闰年的几种方法

C# 清风 847℃ 0评论
闰年
润年

闰年

闰年是公历中的名词,闰年分为普通闰年和世纪闰年。

普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年,1999年不是闰年)

世纪闰年:能被400整除的为世纪闰年。(如2000年是闰年,1900年不是闰年)

闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)。

凡阳历中有闰日(二月为二十九日)的年;闰余(岁余置闰。阴历每年与回归年相比所差的时日)

注意闰年(公历中名词)和闰月(农历中名词)并没有直接的关联,公历中只分闰年和平年,平年有365天,而闰年有366天(2月中多一天)

平年中也可能有闰月(如2017年是平年,农历有闰月,闰6月)。

C#判断闰年的4种方法


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTry
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(DateTime.IsLeapYear(2018));
            Console.WriteLine(IsLeapYear(2018));
            Console.WriteLine(IsLeapYearPlus(2018));
            Console.WriteLine(IsLeapYearConvert(2018));
            Console.Read();
        }

        public static bool IsLeapYear(int year)
        {
            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
            {
                return true;
            }
            return false;
        }
        public static bool IsLeapYearPlus(int year)
        {
            return DateTime.DaysInMonth(year, 2) == 29;
        }
        public static bool IsLeapYearConvert(int year)
        {
            string datestring;
            DateTime dt;
            try
            {
                datestring = $"{year}-02-29";
                dt = Convert.ToDateTime(datestring);
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}



转载请注明:清风亦平凡 » C#判断闰年的几种方法

喜欢 (1)or分享 (0)
支付宝扫码打赏 支付宝扫码打赏 微信打赏 微信打赏
头像
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址