Convert milliseconds to human readable time lapse

2020-05-19 03:43发布

I would like to format some commands execution times in a human readable format, for example:

3 -> 3ms
1100 -> 1s 100ms
62000 -> 1m 2s
etc ..

Taking into account days, hours, minutes, seconds, ...

Is it possible using C#?

10条回答
【Aperson】
2楼-- · 2020-05-19 04:06
public static string ReadableTime(int milliseconds)
{
    var parts = new List<string>();
    Action<int, string> add = (val, unit) => { if (val > 0) parts.Add(val+unit); };
    var t = TimeSpan.FromMilliseconds(milliseconds);

    add(t.Days, "d");
    add(t.Hours, "h");
    add(t.Minutes, "m");
    add(t.Seconds, "s");
    add(t.Milliseconds, "ms");

    return string.Join(" ", parts);
}
查看更多
▲ chillily
3楼-- · 2020-05-19 04:10

This probably has a slightly different output than requested, but the result is human readable - and it can be adapted to fit many other use cases.

private static List<double> _intervals = new List<double>
{
    1.0 / 1000 / 1000,
    1.0 / 1000,
    1,
    1000,
    60 * 1000,
    60 * 60 * 1000
};
private static List<string> _units = new List<string>
{
    "ns",
    "µs",
    "ms",
    "s",
    "min",
    "h"
};

public string FormatUnits(double milliseconds, string format = "#.#")
{
    var interval = _intervals.Last(i=>i<=milliseconds);
    var index = _intervals.IndexOf(interval);

    return string.Concat((milliseconds / interval).ToString(format) , " " , _units[index]);
}

Example calls...

Console.WriteLine(FormatUnits(1));
Console.WriteLine(FormatUnits(20));
Console.WriteLine(FormatUnits(300));
Console.WriteLine(FormatUnits(4000));
Console.WriteLine(FormatUnits(50000));
Console.WriteLine(FormatUnits(600000));
Console.WriteLine(FormatUnits(7000000));
Console.WriteLine(FormatUnits(80000000));

...and results:

1000 µs
20 ms
300 ms
4 s
50 s
10 min
1.9 h
22.2 h
查看更多
来,给爷笑一个
4楼-- · 2020-05-19 04:13

Maybe something like this?

DateTime.Now.ToString("%d 'd' %h 'h' %m 'm' %s 'seconds' %ms 'ms'")
查看更多
放我归山
5楼-- · 2020-05-19 04:16

I know this is old, but I wanted to answer with a great nuget package.

Install-Package Humanizer

https://www.nuget.org/packages/Humanizer

https://github.com/MehdiK/Humanizer

Example from their readme.md

TimeSpan.FromMilliseconds(1299630020).Humanize(4) => "2 weeks, 1 day, 1 hour, 30 seconds"
查看更多
戒情不戒烟
6楼-- · 2020-05-19 04:17

You could utilize the static TimeSpan.FromMilliseconds method as well as the resulting TimeSpan's Days, Hours, Minutes, Seconds and Milliseconds properties.

But I'm busy right now, so I'll leave the rest to you as an exercise.

查看更多
我只想做你的唯一
7楼-- · 2020-05-19 04:19

.NET 4 accepts format in TimeSpan.Tostring().

For other you can implement extension method like

    public static string Format(this TimeSpan obj)
    {
        StringBuilder sb = new StringBuilder();
        if (obj.Hours != 0)
        {
            sb.Append(obj.Hours);
            sb.Append(" "); 
            sb.Append("hours");
            sb.Append(" ");
        }
        if (obj.Minutes != 0 || sb.Length != 0)
        {
            sb.Append(obj.Minutes);
            sb.Append(" "); 
            sb.Append("minutes");
            sb.Append(" ");
        }
        if (obj.Seconds != 0 || sb.Length != 0)
        {
            sb.Append(obj.Seconds);
            sb.Append(" "); 
            sb.Append("seconds");
            sb.Append(" ");
        }
        if (obj.Milliseconds != 0 || sb.Length != 0)
        {
            sb.Append(obj.Milliseconds);
            sb.Append(" "); 
            sb.Append("Milliseconds");
            sb.Append(" ");
        }
        if (sb.Length == 0)
        {
            sb.Append(0);
            sb.Append(" "); 
            sb.Append("Milliseconds");
        }
        return sb.ToString();
    }

and call as

foreach (TimeSpan span in spans)
{
    MessageBox.Show(string.Format("{0}",  span.Format()));
}
查看更多
登录 后发表回答