1 #region 友好显示时间 2 /// <summary> 3 /// 友好显示时间 4 /// </summary> 5 /// <param name="date"></param> 6 /// <returns></returns> 7 public static string ShowTime(DateTime date) 8 { 9 const int SECOND = 1;10 const int MINUTE = 60 * SECOND;11 const int HOUR = 60 * MINUTE;12 const int DAY = 24 * HOUR;13 const int MONTH = 30 * DAY;14 TimeSpan ts = DateTime.Now - date;15 double delta = ts.TotalSeconds;16 if (delta < 0)17 {18 return "not yet";19 }20 if (delta < 1 * MINUTE)21 {22 return ts.Seconds == 1 ? "1秒前" : ts.Seconds + "秒前";23 }24 if (delta < 2 * MINUTE)25 {26 return "1分钟之前";27 }28 if (delta < 45 * MINUTE)29 {30 return ts.Minutes + "分钟";31 }32 if (delta < 90 * MINUTE)33 {34 return "1小时前";35 }36 if (delta < 24 * HOUR)37 {38 return ts.Hours + "小时前";39 }40 if (delta < 48 * HOUR)41 {42 return "昨天";43 }44 if (delta < 30 * DAY)45 {46 return ts.Days + " 天之前";47 }48 if (delta < 12 * MONTH)49 {50 int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));51 return months <= 1 ? "一个月之前" : months + "月之前";52 }53 else54 {55 int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));56 return years <= 1 ? "一年前" : years + "年前";57 }58 }59 #endregion
新闻热点
疑难解答