asp.net显示评论的时候为几天前的格式

自己做的一个小项目实现的功能,做个记录先~

效果如图:


代码如下:

 public static  class TimerHelper
    {
       public static string GetTimeSpan(TimeSpan time)//TimeSpan为两个DateTime进行计算之后产生的类型,使用时传入当前时间-发表评论的时间即可
       {
           if (time.TotalDays >= 365)
           {
               return Math.Floor(time.TotalDays / 365) + "年前";
           }
           else if (time.TotalDays >= 30)
           {
               return Math.Floor(time.TotalDays / 30) + "月前";
           }
           else if (time.TotalDays >= 1)
           {
               return Math.Floor(time.TotalDays) + "天前";
           }
           else if (time.TotalHours >= 1)
           {
               return Math.Floor(time.TotalHours) + "小时前";
           }
           else if (time.TotalMinutes >= 1)
           {
               return Math.Floor(time.TotalMinutes) + "分钟前";
           }
           else
           {
               return "刚刚";
           }
       }
    }

简单明了~还是要记一下吧= =省的忘记

posted @ 2014-07-07 11:46  蒋源德  阅读(192)  评论(0编辑  收藏  举报