asp.net 有关时间各种(输出)处理

时间:2022-01-26 17:23:21

有关一下时间处理方法

         /// <summary>
        /// 将时间转换成几小时、几天之类的格式
        /// </summary>
        /// <param name="maxDays">最多显示几天前</param>
        /// <param name="minuteDif">几分钟之内显示成刚刚</param>
        /// <param name="dateTime">需要转换的时间</param>
        /// <returns></returns>
        public static string ConvertDateToTime(DateTime? dateTime,int maxDays=3,int minuteDif=3)
        {
            string hoursAgo = "小时前";
            string minuteAgo = "分钟前";
            string dayAgo = "天前";
            if (dateTime.HasValue)
            {
                if (dateTime.Value.AddMinutes(minuteDif) > DateTime.Now)//小于设置的时间则显示为刚刚
                {
                    return "刚刚";
                }else if (dateTime.Value.AddMinutes(60) > DateTime.Now)//一小时之内显示分钟
                {
                    int maxMinute = DateTime.Now.Minute;
                    int minMinute=dateTime.Value.Minute;
                    return GetPositiveNumber(minMinute,maxMinute,60)+minuteAgo;
                }else if((dateTime.Value.AddHours(24)>DateTime.Now))//一天之内显示几小时之前
                {
                    int maxHour = DateTime.Now.Hour;
                    int minHour = dateTime.Value.Hour;
                    return GetPositiveNumber(minHour, maxHour, 24) + hoursAgo;
                }else if (dateTime.Value.AddDays(maxDays) > DateTime.Now)//显示几天前
                {
                    int maxDay = DateTime.Now.Day;
                    int minDay = dateTime.Value.Day;
                    return GetPositiveNumber(minDay, maxDay, 30) + dayAgo;
                }
                else
                {
                    return dateTime.Value.Date.ToString("MM-dd");
                }
            }
            return null;
        }
        /// <summary>
        /// 主要计算时间差
        /// </summary>
        /// <param name="minValue">需要换算时间值</param>
        /// <param name="maxValue">当前时间值</param>
        /// <param name="reference">换算率</param>
        /// <returns></returns>
        public static int GetPositiveNumber(int minValue, int maxValue, int reference)
        {
            return maxValue - minValue > 0 ? maxValue - minValue : maxValue - minValue + reference;
        }

        #region 把秒转换成分钟
        /// <summary>
        /// 把秒转换成分钟
        /// </summary>
        /// <returns></returns>
        public static int SecondToMinute(int Second)
        {
            decimal mm = (decimal)((decimal)Second / (decimal)60);
            return Convert.ToInt32(Math.Ceiling(mm));
        } 
        #endregion

        #region 返回某年某月最后一天
        /// <summary>
        /// 返回某年某月最后一天
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public static int GetMonthLastDate(int year, int month)
        {
            DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month));
            int Day = lastDay.Day;
            return Day;
        }
        #endregion

        #region 时分秒转换成秒
        /// <summary>
        /// 时分秒转换成秒
        /// </summary>
        public static int TimeToSecond(int hour, int minute, int second)
        {
            TimeSpan ts = new TimeSpan(hour, minute, second);
            return (int)ts.TotalSeconds;
        } 
        #endregion

        #region 秒转换为时分秒
        /// <summary>
        /// 秒转换为时分秒
        /// </summary>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public static string SecondToDateTime(int seconds)
        {
            TimeSpan ts = new TimeSpan(0, 0, seconds);
            string totalTime = string.Format("{0:00}:{1:00}:{2:00}", (int)ts.TotalHours, ts.Minutes, ts.Seconds);
            return totalTime;// (int)ts.TotalHours + ":" + ts.Minutes + ":" + ts.Seconds;
        } 
        #endregion

        #region 返回时间差
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;
            try
            {
                //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                //TimeSpan ts = ts1.Subtract(ts2).Duration();
                TimeSpan ts = DateTime2 - DateTime1;
                if (ts.Days >= 1)
                {
                    dateDiff = DateTime1.Month.ToString() + "" + DateTime1.Day.ToString() + "";
                }
                else
                {
                    if (ts.Hours > 1)
                    {
                        dateDiff = ts.Hours.ToString() + "小时前";
                    }
                    else
                    {
                        dateDiff = ts.Minutes.ToString() + "分钟前";
                    }
                }
            }
            catch
            { }
            return dateDiff;
        }
        #endregion

        #region 时间格式化
        /// <summary>
        /// 时间格式化
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="format"></param>
        /// <param name="isFormat"></param>
        /// <returns></returns>
        public static string DateTimeFormat(object obj, string format, bool isFormat)
        {
            string str = string.Empty;
            if (null != obj && PageValidate.IsDateTime(obj.ToString()))
            {
                if (isFormat)
                {
                    str = Convert.ToDateTime(obj).ToString(format);
                }
                else
                {
                    str = obj.ToString();
                }
            }
            return str;
        } 
        #endregion