当前位置: 移动技术网 > IT编程>开发语言>c# > c#友好显示日期 c#日期datetime使用方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:#region 友好显示时间        /// <summary>

复制代码 代码如下:

#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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网