簡體   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; }
}

用月計數計算和填充此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