How to find the first day of next month,if the current month is december

前端 未结 10 2029
独厮守ぢ
独厮守ぢ 2021-01-01 11:03

I\'m using the following query to get the next month.

int theMonth = ((System.DateTime)periodStartDate).Month+1;

But if the periodstartDate

10条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 12:05

    DateTime date = DateTime.Now;
    Console.WriteLine(date);
    // Sunday 28.06.2015 г. 10:22:41 ч.
    
    int monthsBack = -1;
    int whichDay = 1;
    // It means -> what day the first day of the previous month is.
    DayOfWeek FirstDayOfWeek = date.AddMonths(monthsBack).AddDays(whichDay).DayOfWeek;
    Console.WriteLine(FirstDayOfWeek);
    // Friday
    
    int delta = DayOfWeek.Monday - date.AddMonths(monthsBack).AddDays(whichDay).DayOfWeek;
    Console.WriteLine(delta);
    // -4
    //-4 ->Monday , -3 ->Tuesday, -2 ->Wednesday , -1 ->Thursday, 0 ->Friday
    

提交回复
热议问题