プログラミングの楽しさ:最初の週の最後の週を取得-変更

4398 ワード

前の1篇は現在の週の初日と最終日を取得して、今日使っている間に小さな問題を発見して、1つの時間の対比をする時、当時は今週の初日が今週の1日の00時からだと考えていませんでした.例えばある記録は
内容:記録、時間:2013-11-04 08:00:00、
12時にこのレコードを照会し、今週の初日を取得したのは2013-11-01 12:00:00で、結果的に記録された時間は常に現在の週の初日より小さい.
だからコードを書くには厳格にしなければなりませんね.今から修正します:初日の時間は0時から統一され、最後の週の時間は23:59:59までです.
 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);


                firstWeekDay = new DateTime(firstWeekDay.Year, firstWeekDay.Month, firstWeekDay.Day);
            }
            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);


                firstWeekDay = new DateTime(firstWeekDay.Year, firstWeekDay.Month, firstWeekDay.Day);
            }
            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);


                lastWeekDay = new DateTime(lastWeekDay.Year, lastWeekDay.Month, lastWeekDay.Day, 23, 59, 59);


            }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);


                lastWeekDay = new DateTime(lastWeekDay.Year, lastWeekDay.Month, lastWeekDay.Day, 23, 59, 59);
            }
            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,23,59,59);
        }
    }