How to make Humanizer not display “no time” if accuracy is greater?

I use Humanizer on TimeSpan with an accuracy of 4:

(dateEnd - dateStart).Humanize(4)

And it generates something like this:

2 hours, 17 minutes, 20 seconds, 141 milliseconds

But when the remaining time is only a few minutes (less than an hour is left), it generates:

17 minutes, 20 seconds, 141 milliseconds, no time

Is there a way to not include this “no time”?

+4
source share
1 answer

I do this to change the accuracy based on the length of the execution time:

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);
+2
source

Source: https://habr.com/ru/post/1546194/


All Articles