c# 擷取某日期所在周的第一天和最後一天

來源:互聯網
上載者:User

標籤:

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.