zoukankan      html  css  js  c++  java
  • asp.net 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前(转载)

     1 public static string DateFormatToString(DateTime dt)  
     2     {  
     3         TimeSpan span = (DateTime.Now - dt).Duration();  
     4         if (span.TotalDays > 60)  
     5         {  
     6             return dt.ToString("yyyy-MM-dd");  
     7         }  
     8         else if (span.TotalDays > 30)  
     9         {  
    10             return "1个月前";  
    11         }  
    12         else if (span.TotalDays > 14)  
    13         {  
    14             return "2周前";  
    15         }  
    16         else if (span.TotalDays > 7)  
    17         {  
    18             return "1周前";  
    19         }  
    20         else if (span.TotalDays > 1)  
    21         {  
    22             return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));  
    23         }  
    24         else if (span.TotalHours > 1)  
    25         {  
    26             return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));  
    27         }  
    28         else if (span.TotalMinutes > 1)  
    29         {  
    30             return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));  
    31         }  
    32         else if (span.TotalSeconds >= 1)  
    33         {  
    34             return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));  
    35         }  
    36         else  
    37         {  
    38             return "1秒前";  
    39         }  
    40     }  

    原文:http://blog.csdn.net/zanychou/article/details/44923497

  • 相关阅读:
    Web Browser使用技巧
    Excel 函数
    删除文件夹, 解决源文件名长度大于文件系统支持的长度问题
    Send Mail using C# code
    动态规划——最长回文子串
    字符串处理总结
    打印日期
    A+B
    对称矩阵
    最小年龄的3个职工
  • 原文地址:https://www.cnblogs.com/OldRion/p/6491024.html
Copyright © 2011-2022 走看看