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

posted @ 2017-03-02 15:56  _daring  阅读(670)  评论(0编辑  收藏  举报