using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace WebAgent.Utils { public static class Extensions { public static string TimeAgo(this DateTime dateTime) { string result; var timeSpan = DateTime.Now.Subtract(dateTime); if (timeSpan <= TimeSpan.FromSeconds(15)) { result = "Just now"; } else if (timeSpan <= TimeSpan.FromSeconds(60)) { result = $"{timeSpan.Seconds} seconds ago"; } else if (timeSpan <= TimeSpan.FromMinutes(60)) { result = timeSpan.Minutes > 1 ? $"{timeSpan.Minutes} minutes ago" : "About a minute ago"; } else if (timeSpan <= TimeSpan.FromHours(24)) { result = timeSpan.Hours > 1 ? $"{timeSpan.Hours} hours ago" : "About an hour ago"; } else if (timeSpan <= TimeSpan.FromDays(30)) { result = timeSpan.Days > 1 ? $"{timeSpan.Days} days ago" : "Yesterday"; } else if (timeSpan <= TimeSpan.FromDays(365)) { result = timeSpan.Days > 30 ? $"{timeSpan.Days / 30} months ago" : "About a month ago"; } else { result = timeSpan.Days > 365 ? $"{timeSpan.Days / 365} years ago" : "About a year ago"; } return result; } } }