C#时间差

 1      #region 获取时间差
 2         /// <summary>
 3         /// 获取时间差
 4         /// </summary>
 5         /// <param name="t">实际时间</param>
 6         /// <returns>时间差</returns>
 7         public static string GetDiffTime(DateTime t)
 8         {
 9             var time = DateTime.Now - t;
10             if (time.TotalDays > 30)
11             {
12                 var currentMonth = Math.Floor(time.TotalDays / 30);
13                 if (currentMonth >= 12)
14                 {
15                     return "1年前";
16                 }
17                 return currentMonth + "个月前";
18             }
19             if (time.TotalHours > 24)
20             {
21                 return Math.Floor(time.TotalDays) + "天前";
22             }
23             if (time.TotalHours > 1)
24             {
25                 return Math.Floor(time.TotalHours) + "小时前";
26             }
27             if (time.TotalMinutes > 1)
28             {
29                 return Math.Floor(time.TotalMinutes) + "分钟前";
30             }
31             return Math.Floor(time.TotalSeconds) + "秒前";
32         }
33 
34         #endregion

 

posted @ 2020-10-14 08:13  b̶i̶n̶g̶.̶  阅读(161)  评论(0编辑  收藏  举报