`
shirlly
  • 浏览: 1652008 次
  • 性别: Icon_minigender_2
  • 来自: 福州
社区版块
存档分类
最新评论

计算本周、近两周、本月、近两个月的开始和结束时间

    博客分类:
  • .NET
 
阅读更多
     #region 日期信息类
        public class DateInfo
        {
            private DateTime beginDate;

            public DateTime BeginDate
            {
                get { return beginDate; }
                set { beginDate = value; }
            }
            private DateTime endDate;

            public DateTime EndDate
            {
                get { return endDate; }
                set { endDate = value; }
            }

        }
        #endregion
        #region 获取本周的时间开始时间和结束时间 add by shirlly 20091218
        /// <summary>
        /// 获取本周的时间开始时间和结束时间
        /// </summary>
        /// <param name="curentDateTime"></param>
        /// <returns></returns>
        public static DateInfo getOneWeekDateInfo(DateTime curentDateTime)
        {
            int year = curentDateTime.Year;
            int month = curentDateTime.Month;
            int day = curentDateTime.Day;
            int weekNum = Week(year, month, day);
            DateInfo weekInfo = new DateInfo();
            weekInfo.BeginDate = curentDateTime.AddDays(-weekNum + 1);
            weekInfo.EndDate = curentDateTime.AddDays(7 - weekNum);
            return weekInfo;
        }
        #endregion

        #region 获取近两周的时间开始时间和结束时间 add by shirlly 20091218
        /// <summary>
        /// 获取近两周的时间开始时间和结束时间
        /// </summary>
        /// <param name="curentDateTime"></param>
        /// <returns></returns>
        public static DateInfo getTwoWeekDateInfo(DateTime curentDateTime)
        {
            int year = curentDateTime.Year;
            int month = curentDateTime.Month;
            int day = curentDateTime.Day;
            int weekNum = Week(year, month, day);
            DateInfo weekInfo = new DateInfo();
            weekInfo.BeginDate = curentDateTime.AddDays(-weekNum + 1 - 7);
            weekInfo.EndDate = curentDateTime.AddDays(7 - weekNum);
            return weekInfo;
        }
        #endregion

        #region 计算本月的开始时间和结束时间 add by shirlly 20091218
        public static DateInfo getOneMonthDateInfo(DateTime curentDateTime)
        {
            int year = curentDateTime.Year;
            int month = curentDateTime.Month;
            int day = curentDateTime.Day;
            int days = DateTime.DaysInMonth(year, month);//计算某年某月的天数
            DateInfo weekInfo = new DateInfo();
            weekInfo.BeginDate = curentDateTime.AddDays(-day + 1);
            weekInfo.EndDate = curentDateTime.AddDays(days - day);
            return weekInfo;
        }
        #endregion

        #region 计算近两个月的开始时间和结束时间 add by shirlly 20091218
        public static DateInfo getTwoMonthDateInfo(DateTime curentDateTime)
        {
            int year = curentDateTime.Year;
            int month = curentDateTime.Month;
            int day = curentDateTime.Day;
            int thisMonthDays = DateTime.DaysInMonth(year, month);//计算某年某月的天数
            if(month - 1<0)//如果是1月 add by shirlly 20100107
            {
                year = year-1;
                month = 12;
            }
            int lastMonthDays = DateTime.DaysInMonth(year, month);
            DateInfo weekInfo = new DateInfo();
            weekInfo.BeginDate = curentDateTime.AddDays((-day + 1) - lastMonthDays);
            weekInfo.EndDate = curentDateTime.AddDays(thisMonthDays - day);
            return weekInfo;
        }
        #endregion

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics