日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

Q1:如何用 C# 计算相对时间 ?

發布時間:2023/12/20 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Q1:如何用 C# 计算相对时间 ? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問:

給定一個 DataTime 值,如何計算如下時間?比如說:

  • 2 小時前?
  • 3 天前?
  • 1 個月前?

答:

  • 我在 DateTime 類上做了一個擴展方法,你可以給它傳遞未來或者過去的時間,還可以給他傳一個 approximation 選項來指定更精細的信息描述,參考如下代碼:
  • using System.Text;/// <summary> /// Compares a supplied date to the current date and generates a friendly English /// comparison ("5 days ago", "5 days from now") /// </summary> /// <param name="date">The date to convert</param> /// <param name="approximate">When off, calculate timespan down to the second. /// When on, approximate to the largest round unit of time.</param> /// <returns></returns> public static string ToRelativeDateString(this DateTime value, bool approximate) {StringBuilder sb = new StringBuilder();string suffix = (value > DateTime.Now) ? " from now" : " ago";TimeSpan timeSpan = new TimeSpan(Math.Abs(DateTime.Now.Subtract(value).Ticks));if (timeSpan.Days > 0){sb.AppendFormat("{0} {1}", timeSpan.Days,(timeSpan.Days > 1) ? "days" : "day");if (approximate) return sb.ToString() + suffix;}if (timeSpan.Hours > 0){sb.AppendFormat("{0}{1} {2}", (sb.Length > 0) ? ", " : string.Empty,timeSpan.Hours, (timeSpan.Hours > 1) ? "hours" : "hour");if (approximate) return sb.ToString() + suffix;}if (timeSpan.Minutes > 0){sb.AppendFormat("{0}{1} {2}", (sb.Length > 0) ? ", " : string.Empty, timeSpan.Minutes, (timeSpan.Minutes > 1) ? "minutes" : "minute");if (approximate) return sb.ToString() + suffix;}if (timeSpan.Seconds > 0){sb.AppendFormat("{0}{1} {2}", (sb.Length > 0) ? ", " : string.Empty, timeSpan.Seconds, (timeSpan.Seconds > 1) ? "seconds" : "second");if (approximate) return sb.ToString() + suffix;}if (sb.Length == 0) return "right now";sb.Append(suffix);return sb.ToString(); }
  • github 上有一個非常流行的 DateTime 幫助類,可以非常精細化的滿足你的要求,參見網址:https://github.com/FluentDateTime/FluentDateTime , 比如下面這些例子:
  • var dateTime1 = 2.Hours().Ago(); var dateTime2 = 3.Days().Ago(); var dateTime3 = 1.Months().Ago(); var dateTime4 = 5.Hours().FromNow(); var dateTime5 = 2.Weeks().FromNow(); var dateTime6 = 40.Seconds().FromNow();
  • 純手工封裝,用 SortedList 預先做一個映射,應該還是能夠滿足你的需求,參考如下代碼。
  • static readonly SortedList<double, Func<TimeSpan, string>> offsets = new SortedList<double, Func<TimeSpan, string>> {{ 0.75, _ => "less than a minute"},{ 1.5, _ => "about a minute"},{ 45, x => $"{x.TotalMinutes:F0} minutes"},{ 90, x => "about an hour"},{ 1440, x => $"about {x.TotalHours:F0} hours"},{ 2880, x => "a day"},{ 43200, x => $"{x.TotalDays:F0} days"},{ 86400, x => "about a month"},{ 525600, x => $"{x.TotalDays / 30:F0} months"},{ 1051200, x => "about a year"},{ double.MaxValue, x => $"{x.TotalDays / 365:F0} years"} };public static string ToRelativeDate(this DateTime input) {TimeSpan x = DateTime.Now - input;string Suffix = x.TotalMinutes > 0 ? " ago" : " from now";x = new TimeSpan(Math.Abs(x.Ticks));return offsets.First(n => x.TotalMinutes < n.Key).Value(x) + Suffix; }

    總結

    以上是生活随笔為你收集整理的Q1:如何用 C# 计算相对时间 ?的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。