SoFunction
Updated on 2025-03-08

c# friendly display date c# dated time usage method


#region Friendly display time
        /// <summary>
/// Friendly display time
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ShowTime(DateTime date)
        {
            const int SECOND = 1;
            const int MINUTE = 60 * SECOND;
            const int HOUR = 60 * MINUTE;
            const int DAY = 24 * HOUR;
            const int MONTH = 30 * DAY;
            TimeSpan ts = - date;
            double delta = ;
            if (delta < 0)
            {
                return "not yet";
            }
            if (delta < 1 * MINUTE)
            {
return == 1 ? "1 second ago" : + "second ago";
            }
            if (delta < 2 * MINUTE)
            {
return "1 minute ago";
            }
            if (delta < 45 * MINUTE)
            {
return + "minutes";
            }
            if (delta < 90 * MINUTE)
            {
return "1 hour ago";
            }
            if (delta < 24 * HOUR)
            {
return + "hour ago";
            }
            if (delta < 48 * HOUR)
            {
return "Yesterday";
            }
            if (delta < 30 * DAY)
            {
return + "before the day";
            }
            if (delta < 12 * MONTH)
            {
                int months = Convert.ToInt32(((double) / 30));
return months <= 1 ? "One month ago" : months + "Before month";
            }
            else
            {
                int years = Convert.ToInt32(((double) / 365));
return years <= 1 ? "One year ago" : years + "One year ago";
            }
        }
        #endregion