Asp.net formatted display time is several months, a few days ago, a few hours ago, a few minutes ago, or a few seconds ago (reproduced), asp.net several hours ago
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 month ago";
11}
12 else if (span.TotalDays> 14)
13 {
14 return "2 weeks ago";
15}
16 else if (span.TotalDays> 7)
17 {
18 return "1 week ago";
19}
20 else if (span.TotalDays> 1)
twenty one {
22 return string.Format ("{0} 天 前", (int) Math.Floor (span.TotalDays));
twenty three }
24 else if (span.TotalHours> 1)
25 {
26 return string.Format ("{0} hours ago", (int) Math.Floor (span.TotalHours));
27}
28 else if (span.TotalMinutes> 1)
29 {
30 return string.Format ("{0} min ago", (int) Math.Floor (span.TotalMinutes));
31}
32 else if (span.TotalSeconds> = 1)
33 {
34 return string.Format ("{0} seconds ago", (int) Math.Floor (span.TotalSeconds));
35}
36 else
37 {
38 return "1 second ago";
39}
40}
Original article: http://blog.csdn.net/zanychou/article/details/44923497