工作中需要计算当前时间对应的本周一、上周一,以下是对应代码:
var datetime = DateTime.Now; int weeknow = Convert.ToInt32(datetime.DayOfWeek); //本周一 int currentMonday = (weeknow == 0 ? 6 : (weeknow - 1)); //上周一 int lastMonday = (weeknow == 0 ? 13 : (weeknow + 6)); string FirstDay = datetime.AddDays(-currentMonday).ToString("yyyy-MM-dd"); string LastFirstDay = datetime.AddDays(-lastMonday).ToString("yyyy-MM-dd");
直接写SQL,没有找到很好的简单有效解决方案,如果有使用SQL解决的,请回复告知。