Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeSpan "pretty time" format in C#

Typing in the title to this question brought me to this question. I'm looking for the same thing, but something perhaps less statically formatted if you get what I mean?

I'm writing a quick program that will be taking a TimeSpan duration of two DateTime objects and outputting them for printing to paper.

The format that would be preferred is: XX days, YY hours, ZZ minutes (seconds are irrelevant; as are days because I don't expect the timespan to incorporate more than a few hours).

Say the following code (as an example):

DateTime start = DateTime.Now;
DateTime end = DateTime.Now.AddMinutes(135);
TimeSpan duration = (end - start);

// output duration as XX hours, YY minutes

Thanks,
Zack

P.S. I also ran across "PrettyTime.NET", but it output things like "3 days ago", etc. Every other topic I found on google to format time duration strings like the above examples lead me to a Java-based solution. :\


UPDATE 2014-09-08:

To follow-up after 3 years, I've found an awesome library on NuGet and Github called "Humanizer" which would solve this issue I was having. I haven't used it that much, but I did try it and it works beautifully. It's quite large of a library though (it emits a whole ton of folders for different cultures to your project's build path (I'm sure there's a way to to customize it)).

like image 498
Zack Avatar asked Mar 25 '11 21:03

Zack


3 Answers

And if you care about pluralization:

public static string ToPrettyFormat(this TimeSpan span) {

    if (span == TimeSpan.Zero) return "0 minutes";

    var sb = new StringBuilder();
    if (span.Days > 0)
        sb.AppendFormat("{0} day{1} ", span.Days, span.Days > 1 ? "s" : String.Empty);
    if (span.Hours > 0)
        sb.AppendFormat("{0} hour{1} ", span.Hours, span.Hours > 1 ? "s" : String.Empty);
    if (span.Minutes > 0)
        sb.AppendFormat("{0} minute{1} ", span.Minutes, span.Minutes > 1 ? "s" : String.Empty);
    return sb.ToString();

}
like image 130
Harry Steinhilber Avatar answered Oct 16 '22 04:10

Harry Steinhilber


You can just output this directly:

 string result = string.Format("{0} days, {1} hours, {2} minutes", duration.Days, duration.Hours, duration.Minutes);

If you are going to be handling "short" times, and you want this to be cleaner, you could do something like:

public string PrettyFormatTimeSpan(TimeSpan span)
{
    if (span.Days > 0)
         return string.Format("{0} days, {1} hours, {2} minutes", span.Days, span.Hours, span.Minutes);
    if (span.Hours > 0)
         return string.Format("{0} hours, {1} minutes", span.Hours, span.Minutes);

    return  string.Format("{0} minutes", span.Minutes);
}
like image 35
Reed Copsey Avatar answered Oct 16 '22 02:10

Reed Copsey


With C# 7:

string FormatTimeSpan(TimeSpan timeSpan)
{
    string FormatPart(int quantity, string name) => quantity > 0 ? $"{quantity} {name}{(quantity > 1 ? "s" : "")}" : null;
    return string.Join(", ", new[] { FormatPart(timeSpan.Days, "day"), FormatPart(timeSpan.Hours, "hour"), FormatPart(timeSpan.Minutes, "minute") }.Where(x => x != null));
}
like image 3
Allen Aston Avatar answered Oct 16 '22 04:10

Allen Aston