我想将输入日期 + 1 年添加到名为完成日期的列中。
如果输入日期是闰年,我需要添加 364 天,如果不是 365 天的话。
有没有办法在 c# 中检查这一点,使用当前日期时间年份并操作闰年/不操作,然后添加天数。
Thanks.
您可以使用日期时间.IsLeapYear http://msdn.microsoft.com/en-us/library/system.datetime.isleapyear.aspx method.
但仅仅为了处理这个问题,您并不真正需要使用该方法。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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)