Calculating time in ASP.NET
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How do I calculate relative time?
当发布某物时,它显示已发布的
例如,漂亮的日期格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | static string GetPrettyDate(DateTime d) { // 1. // Get time span elapsed since the date. TimeSpan s = DateTime.Now.Subtract(d); // 2. // Get total number of days elapsed. int dayDiff = (int)s.TotalDays; // 3. // Get total number of seconds elapsed. int secDiff = (int)s.TotalSeconds; // 4. // Don't allow out of range values. if (dayDiff < 0 || dayDiff >= 31) { return null; } // 5. // Handle same-day times. if (dayDiff == 0) { // A. // Less than one minute ago. if (secDiff < 60) { return"just now"; } // B. // Less than 2 minutes ago. if (secDiff < 120) { return"1 minute ago"; } // C. // Less than one hour ago. if (secDiff < 3600) { return string.Format("{0} minutes ago", Math.Floor((double)secDiff / 60)); } // D. // Less than 2 hours ago. if (secDiff < 7200) { return"1 hour ago"; } // E. // Less than one day ago. if (secDiff < 86400) { return string.Format("{0} hours ago", Math.Floor((double)secDiff / 3600)); } } // 6. // Handle previous days. if (dayDiff == 1) { return"yesterday"; } if (dayDiff < 7) { return string.Format("{0} days ago", dayDiff); } if (dayDiff < 31) { return string.Format("{0} weeks ago", Math.Ceiling((double)dayDiff / 7)); } return null; } |
(您也可以使用名为prettydate的jquery插件。)