C#:实现格式化的时间字符串的最佳方法?

时间:2017-03-06 17:42:02

标签: c# .net datetime system.timers.timer

我正在写这个问题,因为我要求最好的方法来做到这一点。我在我的程序中有很多这些,我想创建一个方法将包含秒的计时器的Int32转换为格式良好的字符串。

因此,例如,如果我的计时器int是at,让我们说一个随机数,如16429 它会是:

4 hours, 32 minutes and 9 seconds

如果是600,那就是:

10 minutes

如果是60,那就是

1 minute

如果是172801,那就是

2 days and 1 second

如果是32,那就是

32 seconds

我希望每个单词末尾的“s”代码如“分钟”,“秒”和“天”只在不等于1的情况下放入S,因此不需要发音正确。如果需要的话,我也只想要几天和几小时以及其他东西,所以如果计时器低于1天(秒),它只显示小时,分钟和秒,或者需要什么。

实现这样的目标的最佳方法是什么?我在下面有这个功能,但它非常凌乱,只能达到几分钟和几秒钟,而不是几小时或几天:

public static string GetConvertedTime(int timer)
{
    int Minutes = timer / 60;
    int Seconds = timer - Minutes * 60;

    if (timer < 60)
    {
        string secs = (Seconds != 1) ? "s" : "";
        return "" + timer + " second" + secs;
    }

    else
    {
        if (Seconds < 1)
        {
            string mins = (Minutes != 1) ? "s" : "";
            return "" + Minutes + " minute" + mins;
        }
        else
        {
            string mins = (Minutes != 1) ? "s" : "";
            string secs = (Seconds != 1) ? "s" : "";
            return "" + Minutes + " minute" + mins + " and " + Seconds + " second" + secs;
        }
    }
}

最好的方法是什么?

3 个答案:

答案 0 :(得分:4)

“最佳方式”非常主观。 DRY通常是最好的,考虑到本地化和非常规复数形式通常是最好的,保持简短和活泼通常是最好的:

public static string FriendlyDuration(int seconds) {
    int[] divisors = { 60 * 60 * 24, 60 * 60, 60 , 1};
    string[] language = { ", ", " and ", "days", "day", "hours", "hour", "minutes", "minute", "seconds", "second"};
    var parts = new List<string>();
    for (int part = 0; part < divisors.Length; ++part) {
        int count = seconds / divisors[part];
        if (count == 0) continue;
        string unit = language[part*2 + (count == 1 ? 3 : 2)];
        parts.Add(string.Format("{0} {1}", count, unit));
        seconds -= count * divisors[part];
    }
    var result = string.Join(language[0], parts.ToArray());
    if (parts.Count > 1) {
        var ix = result.LastIndexOf(language[0]);
        result = result.Substring(0, ix) + language[1] + result.Substring(ix + language[0].Length);
    }
    return result;
}

答案 1 :(得分:3)

我能想到的最好的方法可能是使用TimeSpan,如此:

var time = new TimeSpan(0, 0, timer);
var s = "s";
return $"{time.Days} day{(time.Days != 1 ? s : String.Empty)}, {time.Hours} hour{(time.Hours != 1 ? s : String.Empty)}, {time.Minutes} minute{(time.Minutes != 1 ? s : String.Empty)}, and {time.Seconds} second{(time.Seconds != 1 ? s : String.Empty)}";

如果您需要隐藏空的前导值:

var sb = new StringBuilder();
var s = "s";
var time = new TimeSpan(0, 0, timer);
var continuation = false;
if (time.Days > 0)
{
    sb.Append($"{time.Days} day{(time.Days != 1 ? s : String.Empty)}, ");
    continuation = true;
}
if (time.Hours > 0 || continuation)
{
    sb.Append($"{time.Hours} hour{(time.Hours != 1 ? s : String.Empty)}, ");
    continuation = true;
}
if (time.Minutes > 0 || continuation)
{
    sb.Append($"{time.Minutes} minute{(time.Minutes != 1 ? s : String.Empty)}{(continuation ? "," : String.Empty)} ");
    continuation = true;
}
if (continuation) sb.Append("and ");

sb.Append($"{time.Seconds} second{(time.Seconds != 1 ? s : String.Empty)}");
return sb.ToString();

答案 2 :(得分:1)

您可以尝试名为Humanizer

的此库