Human-readable date formats
Asked Answered
O

4

11

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)";
    }
}
Ordinarily answered 26/12, 2009 at 7:18 Comment(4)
See: https://mcmap.net/q/45143/-calculate-relative-time-in-cTreehopper
I've got a simple algorithm for that on my blog that you could extend: robfe.com/2009/09/timeago-for-csharp Not very different to yours I'm afraid...Stipule
Awesome... I knew someone would be asking this but couldn't figure out what terms to type into the search box.Ordinarily
See also this question: https://mcmap.net/q/45143/-calculate-relative-time-in-c There you'll find several C# implementations for relative time (i.e. "5 mins ago", "10 days ago").Bluet
T
7

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

Teamwork answered 26/12, 2009 at 7:20 Comment(3)
Cool stuff! I'll head over there.Ordinarily
Looks cool. Can you give us an idea where in the Node Time project we should look.Shugart
Have you done it? It's been almost 10 years since :)Discontinuance
D
14

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
Doctrinaire answered 14/10, 2014 at 8:44 Comment(0)
T
7

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

Teamwork answered 26/12, 2009 at 7:20 Comment(3)
Cool stuff! I'll head over there.Ordinarily
Looks cool. Can you give us an idea where in the Node Time project we should look.Shugart
Have you done it? It's been almost 10 years since :)Discontinuance
O
1

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

(Available on NuGet)

Ordinarily answered 17/5, 2011 at 1:29 Comment(1)
It works okay, but can't seem to do future dates (eg, I want it to say "in 2 days")Hallagan
H
0

I ended up using this method as I needed to support future dates like 3 days from now.

Hallagan answered 3/3, 2016 at 23:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.