检查年份是否为leap年 [英] Check to see if year is leap year
本文介绍了检查年份是否为leap年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<script Language="c#" runat="server">
void Page_Load()
{
int currentYear = DateTime.Now.Year();
if (currentYear % 400 == 0) {
Message2.Text = ("This is a leap year");
}
else {
Message2.Text = ("This is not a leap year");
}
}
当前,我遇到了运行时错误。我的目标是使用DateTime.Now.Year()测试当前年份是否为a年。我认为问题是我没有正确将year转换为int?
Currently I am getting a RunTime error. My goal is to test whether or not the current year, using DateTime.Now.Year() is a leap year or not. I think the issue is that I am not properly converting year to int? Please advise.
推荐答案
您可以使用 DateTime.IsLeapYear():
if (DateTime.IsLeapYear(year))
{
//do stuff
}
这篇关于检查年份是否为leap年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文