Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Human-readable date formats

You may have noticed that certain web applications (for example, certain parts of GMail) display dates in a more human-readable format than simply DD/MM/YYYY.

For example, if I open up a mail item from the 23rd (which happens to be 3 days ago at the time of writing, I'll get the following:

Dec 23 (3 days ago)

I'd like to implement similar logic to this in my own web application.

For example, when dealing with a .NET TimeSpan object, I'd like to convert it to text such as the following:

2 months

3 days

Is there a .NET library capable of doing this already?

If not I might build something basic and open-source it.


I've made a basic start here:

public static class TimeSpanHelpers
{
    public static string ToHumanReadableString(
        this TimeSpan timeSpan)
    {
        if (timeSpan.TotalDays > 30)
            return (timeSpan.TotalDays / 30) + " month(s)";

        if (timeSpan.TotalDays > 7)
            return (timeSpan.TotalDays / 7) + " week(s)";

        return (timeSpan.TotalDays) + " day(s)";
    }
}
like image 726
Jonathan Avatar asked Dec 26 '09 07:12

Jonathan


People also ask

How to convert date into Human readable format?

To convert timestamp to human readable format with JavaScript, we can use the date's setTime method. const newDate = new Date(); newDate. setTime(unixtime * 1000); const dateString = newDate. toUTCString();

What formats for displaying date and time?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.

What is D date format?

The "d" standard format specifier represents a custom date and time format string that is defined by a specific culture's DateTimeFormatInfo. ShortDatePattern property. For example, the custom format string that is returned by the ShortDatePattern property of the invariant culture is "MM/dd/yyyy".


3 Answers

Try Humanizer http://humanizr.net/

TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"

// in de-DE culture
TimeSpan.FromDays(1).Humanize() => "Ein Tag"
TimeSpan.FromDays(2).Humanize() => "2 Tage"

// in sk-SK culture
TimeSpan.FromMilliseconds(1).Humanize() => "1 milisekunda"

// and a lot more
DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"
"case".ToQuantity(5) => "5 cases"
"man".ToQuantity(2) => "2 men"
122.ToWords() => "one hundred and twenty-two"
(.5).Gigabytes().Humanize() => "512 MB"
"Long text to truncate".Truncate(10) => "Long text…",
"Sentence casing".Transform(To.TitleCase) => "Sentence Casing"

Nuget:

Install-Package Humanizer
like image 116
Sam7 Avatar answered Oct 23 '22 22:10

Sam7


The Noda Time group is in the process of doing just this. Come on over and join the fun. Forgot to mention the project location Noda Time project

like image 37
James Keesey Avatar answered Oct 23 '22 21:10

James Keesey


Another library for doing this: http://relativetime.codeplex.com/

(Available on NuGet)

like image 39
Jonathan Avatar answered Oct 23 '22 22:10

Jonathan