Calculate relative time in C#

后端 未结 30 2122
生来不讨喜
生来不讨喜 2020-11-21 05:59

Given a specific DateTime value, how do I display relative time, like:

  • 2 hours ago
  • 3 days ago
  • a month ago
30条回答
  •  再見小時候
    2020-11-21 06:18

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public static class RelativeDateHelper
    {
        private static Dictionary> sm_Dict = null;
    
        private static Dictionary> DictionarySetup()
        {
            var dict = new Dictionary>();
            dict.Add(0.75, (mins) => "less than a minute");
            dict.Add(1.5, (mins) => "about a minute");
            dict.Add(45, (mins) => string.Format("{0} minutes", Math.Round(mins)));
            dict.Add(90, (mins) => "about an hour");
            dict.Add(1440, (mins) => string.Format("about {0} hours", Math.Round(Math.Abs(mins / 60)))); // 60 * 24
            dict.Add(2880, (mins) => "a day"); // 60 * 48
            dict.Add(43200, (mins) => string.Format("{0} days", Math.Floor(Math.Abs(mins / 1440)))); // 60 * 24 * 30
            dict.Add(86400, (mins) => "about a month"); // 60 * 24 * 60
            dict.Add(525600, (mins) => string.Format("{0} months", Math.Floor(Math.Abs(mins / 43200)))); // 60 * 24 * 365 
            dict.Add(1051200, (mins) => "about a year"); // 60 * 24 * 365 * 2
            dict.Add(double.MaxValue, (mins) => string.Format("{0} years", Math.Floor(Math.Abs(mins / 525600))));
    
            return dict;
        }
    
        public static string ToRelativeDate(this DateTime input)
        {
            TimeSpan oSpan = DateTime.Now.Subtract(input);
            double TotalMinutes = oSpan.TotalMinutes;
            string Suffix = " ago";
    
            if (TotalMinutes < 0.0)
            {
                TotalMinutes = Math.Abs(TotalMinutes);
                Suffix = " from now";
            }
    
            if (null == sm_Dict)
                sm_Dict = DictionarySetup();
    
            return sm_Dict.First(n => TotalMinutes < n.Key).Value.Invoke(TotalMinutes) + Suffix;
        }
    }
    

    The same as another answer to this question but as an extension method with a static dictionary.

提交回复
热议问题