Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Humanizer not display "no time" if the precision is bigger?

Tags:

c#

humanizer

I am using Humanizer on a TimeSpan with precision 4:

(dateEnd - dateStart).Humanize(4)

And it generates something like this:

2 hours, 17 minutes, 20 seconds, 141 milliseconds

But when the time remaining is just minutes (less than a hour left) it generates:

17 minutes, 20 seconds, 141 milliseconds, no time

Is there a way to not include this "no time"?

like image 963
BrunoLM Avatar asked Oct 01 '22 11:10

BrunoLM


1 Answers

I'm doing this to change the precision based on the run time length:

TimeSpan runTime = dateEnd - dateStart;

if (runTime.TotalMinutes < 1)
{
    precision = 1; //49 seconds
}
else if (runTime.TotalHours < 1)
{
    precision = 2; //27 minutes, 49 seconds
}
else
{
    precision = 3; //1 day, 2 hours, 27 minutes  OR  2 hours, 27 minutes, 49 seconds
}

runTime.Humanize(precision);
like image 142
Homer Avatar answered Oct 04 '22 18:10

Homer