C# 获取时间差(几天前,几小时前,几分钟前,几秒前)

 1         #region 获取时间差string GetTime(BsonString getTime)
 2         /// <summary>
 3         /// 获取时间差
 4         /// </summary>
 5         /// <param name="getTime">数据库时间</param>
 6         /// <returns>时间差</returns>
 7         private string GetTime(BsonString getTime)
 8         {
 9             var time1 = DateTime.ParseExact(Convert.ToString(getTime), "yyyy-MM-dd HH:mm:ss", null);
10             var time = DateTime.Now - time1;
11 
12             if (time.TotalHours > 24)
13             {
14                 return Math.Floor(time.TotalDays) + "天前";
15             }
16             if (time.TotalHours > 1)
17             {
18                 return Math.Floor(time.TotalHours) + "小时前";
19             }
20             if (time.TotalMinutes > 1)
21             {
22                 return Math.Floor(time.TotalMinutes) + "分钟前";
23             }
24             return Math.Floor(time.TotalSeconds) + "秒前";
25         }
26         #endregion

 

posted @ 2015-03-12 09:53  b̶i̶n̶g̶.̶  阅读(1851)  评论(0编辑  收藏  举报