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);
        }

    }


我在这里想念什么

最佳答案

startDate.AddYears(1).AddDays(-1);怎么样?

假设给定的开始日期为7月1日,则您希望结束日期为6月30日。您发布的示例建议您希望为7月1日-> 7月1日,在这种情况下,它将为startDate.AddYears(1);

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

10-09 13:09