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

效果如图:

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 "刚刚";
           }
       }
    }

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

相关文章:

  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案