加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#友好显示日期 c#日期datetime使用方法

发布时间:2020-12-15 05:54:31 所属栏目:百科 来源:网络整理
导读:复制代码 代码如下: #region 友好显示时间 /// summary /// 友好显示时间 /// /summary /// param name="date"/param /// returns/returns public static string ShowTime(DateTime date) { const int SECOND = 1; const int MINUTE = 60 * SECOND; const in

复制代码 代码如下:

#region 友好显示时间
        /// <summary>
        /// 友好显示时间
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ShowTime(DateTime date)
        {
            const int SECOND = 1;
            const int MINUTE = 60 * SECOND;
            const int HOUR = 60 * MINUTE;
            const int DAY = 24 * HOUR;
            const int MONTH = 30 * DAY;
            TimeSpan ts = DateTime.Now - date;
            double delta = ts.TotalSeconds;
            if (delta < 0)
            {
                return "not yet";
            }
            if (delta < 1 * MINUTE)
            {
                return ts.Seconds == 1 ? "1秒前" : ts.Seconds + "秒前";
            }
            if (delta < 2 * MINUTE)
            {
                return "1分钟之前";
            }
            if (delta < 45 * MINUTE)
            {
                return ts.Minutes + "分钟";
            }
            if (delta < 90 * MINUTE)
            {
                return "1小时前";
            }
            if (delta < 24 * HOUR)
            {
                return ts.Hours + "小时前";
            }
            if (delta < 48 * HOUR)
            {
                return "昨天";
            }
            if (delta < 30 * DAY)
            {
                return ts.Days + " 天之前";
            }
            if (delta < 12 * MONTH)
            {
                int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
                return months <= 1 ? "一个月之前" : months + "月之前";
            }
            else
            {
                int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
                return years <= 1 ? "一年前" : years + "年前";
            }
        }
        #endregion

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读