检查C#中的闰年

Sha*_*500 17 c#

我想在一个名为完成日期的列中添加输入日期+ 1年.

如果输入日期是闰年,我需要增加364天,如果不是365天.

有没有办法在c#中检查这个,使用当前日期时间的年份和操纵闰年/不,然后添加日期.

谢谢.

Pat*_*tko 44

您可以使用DateTime.IsLeapYear方法.

但只是为了处理你真的不需要使用那种方法.DateTime.AddYears把闰年考虑在内.

var leapYear = new DateTime(2000, 2, 29);
Console.WriteLine("Is 2000 a leap year? {0}", DateTime.IsLeapYear(leapYear.Year)); // 2000 is a leap year
Console.WriteLine("One year added to {0} is {1}", leapYear, leapYear.AddYears(1)); // 2000-02-29 plus 1 year is 2001-02-28
Run Code Online (Sandbox Code Playgroud)