获取特定月份计数的开始日期和结束日期

时间:2015-05-14 10:10:11

标签: c# datetime

int months = applicationLookBack.LookBackPeriod; //In Months

public class DateRange 
{
     public virtual DateTime? Startdate { get; set; }
     public virtual DateTime? Enddate { get; set; }
}

计算和填充此DateRange的最佳方法是什么 月数?

1 个答案:

答案 0 :(得分:1)

    int months = 14;
    int currentYear = DateTime.Now.Year;

    DateTime firstDate = new DateTime(currentYear, 1, 1);
    DateTime lastDate = firstDate.AddMonths(months - 1);

    var lastMonthDays = DateTime.DaysInMonth(lastDate.Year, lastDate.Month);
    lastDate = lastDate.AddDays(lastMonthDays - 1);
相关问题