首页 > 代码库 > c# 获取某日期所在周的第一天和最后一天
c# 获取某日期所在周的第一天和最后一天
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WyfClass{public class tools{/// <summary>/// 得到本周第一天(以星期天为第一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekFirstDaySun(DateTime datetime){ //星期天为第一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); int daydiff = (-1) * weeknow; //本周第一天 string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(FirstDay);}/// <summary>/// 得到本周第一天(以星期一为第一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekFirstDayMon(DateTime datetime){ //星期一为第一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); //因为是以星期一为第一天,所以要判断weeknow等于0时,要向前推6天。 weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1)); int daydiff = (-1) * weeknow; //本周第一天 string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(FirstDay);}/// <summary>/// 得到本周最后一天(以星期六为最后一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekLastDaySat(DateTime datetime){ //星期六为最后一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); int daydiff = (7 - weeknow) - 1; //本周最后一天 string LastDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(LastDay);}/// <summary>/// 得到本周最后一天(以星期天为最后一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekLastDaySun(DateTime datetime){ //星期天为最后一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); weeknow = (weeknow == 0 ? 7 : weeknow); int daydiff = (7 - weeknow); //本周最后一天 string LastDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(LastDay);}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WyfClass{public class tools{/// <summary>/// 得到本周第一天(以星期天为第一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekFirstDaySun(DateTime datetime){ //星期天为第一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); int daydiff = (-1) * weeknow; //本周第一天 string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(FirstDay);}/// <summary>/// 得到本周第一天(以星期一为第一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekFirstDayMon(DateTime datetime){ //星期一为第一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); //因为是以星期一为第一天,所以要判断weeknow等于0时,要向前推6天。 weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1)); int daydiff = (-1) * weeknow; //本周第一天 string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(FirstDay);}/// <summary>/// 得到本周最后一天(以星期六为最后一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekLastDaySat(DateTime datetime){ //星期六为最后一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); int daydiff = (7 - weeknow) - 1; //本周最后一天 string LastDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(LastDay);}/// <summary>/// 得到本周最后一天(以星期天为最后一天)/// </summary>/// <param name="datetime"></param>/// <returns></returns>public DateTime GetWeekLastDaySun(DateTime datetime){ //星期天为最后一天 int weeknow = Convert.ToInt32(datetime.DayOfWeek); weeknow = (weeknow == 0 ? 7 : weeknow); int daydiff = (7 - weeknow); //本周最后一天 string LastDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd"); return Convert.ToDateTime(LastDay);}}}
c# 获取某日期所在周的第一天和最后一天
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。