Getting first and last day of the current month

前端 未结 6 1147
独厮守ぢ
独厮守ぢ 2020-12-13 06:15

I have here 2 datepicker for start date and end date.

how can I get the first day and last day of the current month



        
6条回答
  •  一个人的身影
    2020-12-13 06:23

    var now = DateTime.Now;
    var first = new DateTime(now.Year, now.Month, 1);
    var last = first.AddMonths(1).AddDays(-1);
    

    You could also use DateTime.DaysInMonth method:

    var last = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
    

提交回复
热议问题