首先,我们需要确认您提供的值是否为有效年份。在 C# 中,您可以使用 DateTime.ParseExact 函数将字符串转换为 DateTime 对象,然后使用 DateTime.IsLeapYear 方法检查年份是否为闰年。以下是一个示例代码:
string year = "2022";
if (DateTime.ParseExact(year, "yyyy", null).IsLeapYear())
{
Console.WriteLine("The year is a leap year.");
}
else
{
Console.WriteLine("The year is not a leap year.");
}
如果 year 变量的值为 2022,则输出为 "The year is a leap year.",否则输出为 "The year is not a leap year."。
领取专属 10元无门槛券
手把手带您无忧上云