編程樂趣:C#擷取日期所在周、月份第一和最後一天

來源:互聯網
上載者:User

寫了個小功能,需要用到以周為時間段,於是寫了個擷取周第一和最後一天的方法,擷取月份的第一和最後一天就比較簡單了。代碼如下:

  public class DateTimeTool    {        /// <summary>        /// 擷取指定日期所在周的第一天,星期天為第一天        /// </summary>        /// <param name="dateTime"></param>        /// <returns></returns>        public static DateTime GetDateTimeWeekFirstDaySun(DateTime dateTime)        {            DateTime firstWeekDay = DateTime.Now;            try            {                //得到是星期幾,然後從當前日期減去相應天數                 int weeknow = Convert.ToInt32(dateTime.DayOfWeek);                int daydiff = (-1) * weeknow;                firstWeekDay = dateTime.AddDays(daydiff);            }            catch { }            return firstWeekDay;        }        /// <summary>        /// 擷取指定日期所在周的第一天,星期一為第一天        /// </summary>        /// <param name="dateTime"></param>        /// <returns></returns>        public static DateTime GetDateTimeWeekFirstDayMon(DateTime dateTime)        {            DateTime firstWeekDay = DateTime.Now;            try            {                int weeknow = Convert.ToInt32(dateTime.DayOfWeek);                //星期一為第一天,weeknow等於0時,要向前推6天。                   weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));                int daydiff = (-1) * weeknow;                firstWeekDay = dateTime.AddDays(daydiff);            }            catch { }            return firstWeekDay;        }        /// <summary>        /// 擷取指定日期所在周的最後一天,星期六為最後一天        /// </summary>        /// <param name="dateTime"></param>        /// <returns></returns>        public static DateTime GetDateTimeWeekLastDaySat(DateTime dateTime)        {            DateTime lastWeekDay = DateTime.Now;            try            {                int weeknow = Convert.ToInt32(dateTime.DayOfWeek);                int daydiff = (7 - weeknow) - 1;                lastWeekDay = dateTime.AddDays(daydiff);            }catch { }            return lastWeekDay;        }        /// <summary>        /// 擷取指定日期所在周的最後一天,星期天為最後一天        /// </summary>        /// <param name="dateTime"></param>        /// <returns></returns>        public static DateTime GetDateTimeWeekLastDaySun(DateTime dateTime)        {            DateTime lastWeekDay = DateTime.Now;            try            {                int weeknow = Convert.ToInt32(dateTime.DayOfWeek);                weeknow = (weeknow == 0 ? 7 : weeknow);                int daydiff = (7 - weeknow);                 lastWeekDay = dateTime.AddDays(daydiff);            }            catch { }            return lastWeekDay;        }        /// <summary>        /// 擷取指定日期的月份第一天        /// </summary>        /// <param name="dateTime"></param>        /// <returns></returns>        public static DateTime GetDateTimeMonthFirstDay(DateTime dateTime)        {            if (dateTime == null)            {                dateTime = DateTime.Now;            }            return new DateTime(dateTime.Year, dateTime.Month, 1);        }        /// <summary>        /// 擷取指定月份最後一天        /// </summary>        /// <param name="dateTime"></param>        /// <returns></returns>        public static DateTime GetDateTimeMonthLastDay(DateTime dateTime)        {            int day = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);            return new DateTime(dateTime.Year, dateTime.Month, day);        }    }


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.