简体   繁体   English

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

[英]Get Start Date and End date for certain month count

int months = applicationLookBack.LookBackPeriod; //In Months

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

What will be the best way to calculate and fill this DateRange with the month count? 用月计数计算和填充此DateRange的最佳方法是什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM