Calculate previous week's start and end date

前端 未结 7 1681
甜味超标
甜味超标 2020-12-13 17:41

What is the best way to calculate the previous week\'s start and end date in C#? I.e. today 18 March would result in 9 March (Monday last week) and 15 March (Sunday last we

7条回答
  •  遥遥无期
    2020-12-13 18:04

    You can skip the while loop and use

    DateTime mondayOfLastWeek = date.AddDays( -(int)date.DayOfWeek - 6 );
    

    This assumes you're using Monday as the first day of the week.

提交回复
热议问题