给定一个DateTime和一个DayOfWeek应该返回该月最后一个DayOfWeek的日期。

例如。
2009年3月1日和星期日将返回2009年3月29日

最佳答案

找不到方便的单缸纸,但此方法可行:

static DateTime LastDayOfWeekInMonth(DateTime day, DayOfWeek dow)
{
    DateTime lastDay = new DateTime(day.Year, day.Month, 1).AddMonths(1).AddDays(-1);
    DayOfWeek lastDow = lastDay.DayOfWeek;

    int diff = dow - lastDow;

    if (diff > 0) diff -= 7;

    System.Diagnostics.Debug.Assert(diff <= 0);

    return lastDay.AddDays(diff);
}

关于c# - 计算一个月的最后一个DayOfWeek,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/623459/

10-11 23:55