find week ending date of last completed week

后端 未结 5 923
独厮守ぢ
独厮守ぢ 2021-01-04 07:04

For any given date, how would you find the week ending date of the last completed week, if your week runs from Sunday to Saturday?

5条回答
  •  情话喂你
    2021-01-04 07:54

    If you want to specify which day is the end of week, and you don't want to worry about what day the system has defined as the default start of the week, use this method:

    private static DateTime GetPreviousSpecifiedDayOfWeek(DateTime dt, DayOfWeek day)
        {
            if (dt.DayOfWeek == day)
            {
                return dt;
            }
    
            while (dt.DayOfWeek != day)
            {
                dt = dt.AddDays(-1);
            }
    
            return dt;
        }
    

提交回复
热议问题