gpt4 book ai didi

c# - 给定开始日期如何根据闰年找到有 365 或 366 天的结束日期

转载 作者:行者123 更新时间:2023-11-30 20:07:48 25 4
gpt4 key购买 nike

public static DateTime GetBudgetYearEndDateConsideringLeapYear(DateTime budgetYearStartDate)
{
DateTime endDate = DateTime.MinValue;
if (budgetYearStartDate == null)
throw new ArgumentNullException("budgetYearStartDate must be provided before calling this method");

int startingDateMonth = budgetYearStartDate.Month;
if (startingDateMonth > 2) /// Leap year for the starting year is escaped because Month is NOT February
{
endDate = budgetYearStartDate.AddDays(365);
bool isEndYearLeapYear = DateTime.IsLeapYear(endDate.Year);

if (isEndYearLeapYear)
{
return endDate.AddDays(1);
}
return endDate;
}
else // started from JAN/ FEB
{
if (DateTime.IsLeapYear(budgetYearStartDate.Year))
{
return budgetYearStartDate.AddDays(366);
}
return budgetYearStartDate.AddDays(365);
}

}

我在这里错过了什么

最佳答案

有什么问题:

public static DateTime GetBudgetYearEndDateConsideringLeapYear
(DateTime budgetYearStartDate)
{
return budgetYearStartDate.AddYears(1);
}

?换句话说,返回从开始日期算起一年的日期,不管它是否是闰年。

有几点需要注意:

  • 如果开始日期是 2 月 29 日,您应该考虑您想要的结果
  • 鉴于 DateTime 是一个值类型
  • ,您的无效检查毫无意义

关于c# - 给定开始日期如何根据闰年找到有 365 或 366 天的结束日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7998830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com