Esempio n. 1
0
        /// <summary>
        /// 与指定的时间计算时间差(date-theDate)
        /// </summary>
        /// <param name="theTime">被计算日期</param>
        /// <param name="date">计算日期</param>
        /// <param name="calcTime">是事计算时间部分,默认true</param>
        /// <param name="calcHour">是否计算小时,默认true</param>
        /// <param name="calcMinute">是否计算分,默认true</param>
        /// <param name="calcSecond">是否计算秒,默认true</param>
        /// <returns></returns>
        public static TimeInfo GetTimeDiff(this DateTime theTime, DateTime date,
                                           bool calcTime = true, bool calcHour = true, bool calcMinute = true, bool calcSecond = true)
        {
            var timeinfo = new TimeInfo();

            timeinfo.Source = theTime;
            timeinfo.Now    = date;
            DateTime?temp = null;

            if (date < theTime)
            {
                temp    = theTime;
                theTime = date;
                date    = temp.Value;
            }

            #region 计算秒
            if (calcSecond && calcTime)
            {
                timeinfo.Second = date.Second - theTime.Second;
                if (timeinfo.Second < 0)
                {
                    timeinfo.Second += 60;
                    timeinfo.Minute -= 1;
                }
            }
            #endregion

            #region 计算分
            if (calcSecond && calcTime)
            {
                timeinfo.Minute += date.Minute - theTime.Minute;
                if (timeinfo.Minute < 0)
                {
                    timeinfo.Minute += 60;
                    timeinfo.Hour   -= 1;
                }
            }
            #endregion

            #region 计算时
            if (calcHour && calcTime)
            {
                timeinfo.Hour += date.Hour - theTime.Hour;
                if (timeinfo.Hour < 0)
                {
                    timeinfo.Hour += 24;
                    timeinfo.Day  -= 1;
                }
            }
            #endregion

            #region 计算天
            var theMonthDays = DateTime.DaysInMonth(theTime.Year, theTime.Month);
            // 判断出生日期和计算日期是不是都是月份的最后一天
            // 出生月总天数大于当前月总天数的月底做满月处理,
            // 如: 2000-03-31 到 2000-04-30 做为满月,5月份则要31号才做为满月,不计算天数
            if (!(theTime.Day + timeinfo.Day == theMonthDays && date.Day == DateTime.DaysInMonth(date.Year, date.Month)))
            {
                timeinfo.Day += date.Day - theTime.Day;
            }
            if (timeinfo.Day < 0)
            {
                var nowPrevMonth  = date.AddMonths(-1); // 当前日期上月日期
                var prevMonthDays = DateTime.DaysInMonth(nowPrevMonth.Year, nowPrevMonth.Month);
                // 如果借过来的月的总天数小于被减月份的总天数,则以被减月份的总天数为准
                // 如:  2013-01-20 到 2013-03-10 , 此时借的是2月份, 小于出生月1月份的总天数,则天数为 10-20+31=21天
                timeinfo.Day   += Math.Max(prevMonthDays, theMonthDays);
                timeinfo.Month -= 1;
            }
            #endregion

            #region 计算月
            timeinfo.Month += date.Month - theTime.Month;
            if (timeinfo.Month < 0) // 月数不足借年
            {
                timeinfo.Month += 12;
                timeinfo.Year  -= 1;
            }
            #endregion

            #region 计算年
            timeinfo.Year += date.Year - theTime.Year;
            #endregion

            if (temp.HasValue)
            {
                timeinfo.Second *= -1;
                timeinfo.Minute *= -1;
                timeinfo.Hour   *= -1;
                timeinfo.Day    *= -1;
                timeinfo.Month  *= -1;
                timeinfo.Year   *= -1;
            }
            return(timeinfo);
        }
Esempio n. 2
0
        /// <summary>
        /// 与指定的时间计算时间差(date-theDate)
        /// </summary>
        /// <param name="theTime">被计算日期</param>
        /// <param name="date">计算日期</param>
        /// <param name="calcTime">是事计算时间部分,默认true</param>
        /// <param name="calcHour">是否计算小时,默认true</param>
        /// <param name="calcMinute">是否计算分,默认true</param>
        /// <param name="calcSecond">是否计算秒,默认true</param>
        /// <returns></returns>
        public static TimeInfo GetTimeDiff(this DateTime theTime, DateTime date,
            bool calcTime = true, bool calcHour = true, bool calcMinute = true, bool calcSecond = true)
        {
            var timeinfo = new TimeInfo();
            timeinfo.Source = theTime;
            timeinfo.Now = date;
            DateTime? temp = null;
            if (date < theTime)
            {
                temp = theTime;
                theTime = date;
                date = temp.Value;
            }

            #region 计算秒
            if (calcSecond && calcTime)
            {
                timeinfo.Second = date.Second - theTime.Second;
                if (timeinfo.Second < 0)
                {
                    timeinfo.Second += 60;
                    timeinfo.Minute -= 1;
                }
            }
            #endregion

            #region 计算分
            if (calcSecond && calcTime)
            {
                timeinfo.Minute += date.Minute - theTime.Minute;
                if (timeinfo.Minute < 0)
                {
                    timeinfo.Minute += 60;
                    timeinfo.Hour -= 1;
                }
            }
            #endregion

            #region 计算时
            if (calcHour && calcTime)
            {
                timeinfo.Hour += date.Hour - theTime.Hour;
                if (timeinfo.Hour < 0)
                {
                    timeinfo.Hour += 24;
                    timeinfo.Day -= 1;
                }
            }
            #endregion

            #region 计算天
            var theMonthDays = DateTime.DaysInMonth(theTime.Year, theTime.Month);
            // 判断出生日期和计算日期是不是都是月份的最后一天
            // 出生月总天数大于当前月总天数的月底做满月处理,
            // 如: 2000-03-31 到 2000-04-30 做为满月,5月份则要31号才做为满月,不计算天数
            if (!(theTime.Day + timeinfo.Day == theMonthDays && date.Day == DateTime.DaysInMonth(date.Year, date.Month)))
            {
                timeinfo.Day += date.Day - theTime.Day;
            }
            if (timeinfo.Day < 0)
            {
                var nowPrevMonth = date.AddMonths(-1); // 当前日期上月日期
                var prevMonthDays = DateTime.DaysInMonth(nowPrevMonth.Year, nowPrevMonth.Month);
                // 如果借过来的月的总天数小于被减月份的总天数,则以被减月份的总天数为准
                // 如:  2013-01-20 到 2013-03-10 , 此时借的是2月份, 小于出生月1月份的总天数,则天数为 10-20+31=21天
                timeinfo.Day += Math.Max(prevMonthDays, theMonthDays);
                timeinfo.Month -= 1;
            }
            #endregion

            #region 计算月
            timeinfo.Month += date.Month - theTime.Month;
            if (timeinfo.Month < 0) // 月数不足借年
            {
                timeinfo.Month += 12;
                timeinfo.Year -= 1;
            }
            #endregion

            #region 计算年
            timeinfo.Year += date.Year - theTime.Year;
            #endregion

            if (temp.HasValue)
            {
                timeinfo.Second *= -1;
                timeinfo.Minute *= -1;
                timeinfo.Hour *= -1;
                timeinfo.Day *= -1;
                timeinfo.Month *= -1;
                timeinfo.Year *= -1;
            }
            return timeinfo;
        }