Home
C#
Date With Pretty Format (Hours or Minutes Ago)
This page was last reviewed on Dec 22, 2023.
Dot Net Perls
Pretty date. A date that is nicely-formatted is easier to read. With a C# method, we can show "1 hour ago" or "1 minute ago", instead of seconds.
Method notes. We can implement a pretty date method for use in ASP.NET or Windows programs. This can improve websites and console programs.
DateTime
Example. Prettily-formatted dates can improve a program. To display usable relative and pretty dates, you need to calculate the time elapsed from the original date.
So The method here receives the original DateTime, and then uses DateTime.Now to calculate the elapsed time.
DateTime.Now
Detail The first steps in the GetPrettyDate method take the elapsed days and seconds from the original date.
Detail Here we handle recent dates, in the same day. There is a chain of conditionals that return pretty strings.
Detail Finally, we handle more distant dates. The method here doesn't handle months and years.
using System; class Program { static void Main() { // Test 90 seconds ago. Console.WriteLine(GetPrettyDate( DateTime.Now.AddSeconds(-90))); // Test 25 minutes ago. Console.WriteLine(GetPrettyDate( DateTime.Now.AddMinutes(-25))); // Test 45 minutes ago. Console.WriteLine(GetPrettyDate( DateTime.Now.AddMinutes(-45))); // Test 4 hours ago. Console.WriteLine(GetPrettyDate( DateTime.Now.AddHours(-4))); // Test 15 days ago. Console.WriteLine(GetPrettyDate( DateTime.Now.AddDays(-15))); } static string GetPrettyDate(DateTime d) { // 1. // Get time span elapsed since the date. TimeSpan s = DateTime.Now.Subtract(d); // 2. // Get total number of days elapsed. int dayDiff = (int)s.TotalDays; // 3. // Get total number of seconds elapsed. int secDiff = (int)s.TotalSeconds; // 4. // Don't allow out of range values. if (dayDiff < 0 || dayDiff >= 31) { return null; } // 5. // Handle same-day times. if (dayDiff == 0) { // A. // Less than one minute ago. if (secDiff < 60) { return "just now"; } // B. // Less than 2 minutes ago. if (secDiff < 120) { return "1 minute ago"; } // C. // Less than one hour ago. if (secDiff < 3600) { return string.Format("{0} minutes ago", Math.Floor((double)secDiff / 60)); } // D. // Less than 2 hours ago. if (secDiff < 7200) { return "1 hour ago"; } // E. // Less than one day ago. if (secDiff < 86400) { return string.Format("{0} hours ago", Math.Floor((double)secDiff / 3600)); } } // 6. // Handle previous days. if (dayDiff == 1) { return "yesterday"; } if (dayDiff < 7) { return string.Format("{0} days ago", dayDiff); } if (dayDiff < 31) { return string.Format("{0} weeks ago", Math.Ceiling((double)dayDiff / 7)); } return null; } }
1 minute ago 25 minutes ago 45 minutes ago 4 hours ago 3 weeks ago
A summary. We looked at how to display well-formatted and human-readable pretty dates. This code is mainly a finishing touch for your applications. It can help complete a project.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 22, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.