【C#日期系列(三)】--C#获取某个月的第一个星期几的年月日
需要获取某个月的第一个星期几的年月日
简单写了一个算法
#region 计算每月第一个星期1-7是各是几号 /// <summary> /// 计算每月第一个星期1-7是各是几号 /// </summary> /// <param name="monthStratTime"></param> /// <param name="calcWeek">DayOfWeek枚举 注意周日枚举为0</param> /// <returns></returns> public static DateTime GetEachMonthFirstWeek(DateTime monthStratTime, DayOfWeek calcWeek) { DayOfWeek week = monthStratTime.DayOfWeek;//第一天是星期几 int result = 0; if (calcWeek == DayOfWeek.Sunday) { result = (8 - (int)week) % 7 + 7; } else { result = (8 - (int)week) % 7 + (int)calcWeek; } if (result > 7) { result = result - 7; } monthStratTime = monthStratTime.AddDays(result-1); return monthStratTime; } #endregion