时间跨度格式化

时间:2009-05-08 14:41:09

标签: c# .net timespan

如果您将其声明为“1小时10分钟”,您如何优雅地格式化时间跨度:

TimeSpan t = new TimeSpan(0, 70, 0);

我当然知道你可以为此做一些简单的数学运算,但我有点希望.NET中有一些东西可以帮我处理 - 对于更复杂的场景

<{3}}

重复

5 个答案:

答案 0 :(得分:21)

没有内置功能,您需要使用自定义方法,例如:

TimeSpan ts = new TimeSpan(0, 70, 0);
String.Format("{0} hour{1} {2} minute{3}", 
              ts.Hours, 
              ts.Hours == 1 ? "" : "s",
              ts.Minutes, 
              ts.Minutes == 1 ? "" : "s")

答案 1 :(得分:13)

public static string Pluralize(int n, string unit)
{
    if (string.IsNullOrEmpty(unit)) return string.Empty;

    n = Math.Abs(n); // -1 should be singular, too

    return unit + (n == 1 ? string.Empty : "s");
}

public static string TimeSpanInWords(TimeSpan aTimeSpan)
{
    List<string> timeStrings = new List<string>();

    int[] timeParts = new[] { aTimeSpan.Days, aTimeSpan.Hours, aTimeSpan.Minutes, aTimeSpan.Seconds };
    string[] timeUnits = new[] { "day", "hour", "minute", "second" };

    for (int i = 0; i < timeParts.Length; i++)
    {
        if (timeParts[i] > 0)
        {
            timeStrings.Add(string.Format("{0} {1}", timeParts[i], Pluralize(timeParts[i], timeUnits[i])));
        }
    }

    return timeStrings.Count != 0 ? string.Join(", ", timeStrings.ToArray()) : "0 seconds";
}

答案 2 :(得分:5)

从这里复制我自己的答案:How do I convert a TimeSpan to a formatted string?

public static string ToReadableAgeString(this TimeSpan span)
{
    return string.Format("{0:0}", span.Days / 365.25);
}

public static string ToReadableString(this TimeSpan span)
{
    string formatted = string.Format("{0}{1}{2}{3}",
        span.Duration().Days > 0 ? string.Format("{0:0} days, ", span.Days) : string.Empty,
        span.Duration().Hours > 0 ? string.Format("{0:0} hours, ", span.Hours) : string.Empty,
        span.Duration().Minutes > 0 ? string.Format("{0:0} minutes, ", span.Minutes) : string.Empty,
        span.Duration().Seconds > 0 ? string.Format("{0:0} seconds", span.Seconds) : string.Empty);

    if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);

    if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";

    return formatted;
}

答案 3 :(得分:1)

public static string GetDurationInWords( TimeSpan aTimeSpan )
{
    string timeTaken = string.Empty;

    if( aTimeSpan.Days > 0 )
        timeTaken += aTimeSpan.Days + " day" + ( aTimeSpan.Days > 1 ? "s" : "" );

    if( aTimeSpan.Hours > 0 )
    {
        if( !string.IsNullOrEmpty( timeTaken ) )
           timeTaken += " ";
        timeTaken += aTimeSpan.Hours + " hour" + ( aTimeSpan.Hours > 1 ? "s" : "" );
    }

    if( aTimeSpan.Minutes > 0 )
    {
       if( !string.IsNullOrEmpty( timeTaken ) )
           timeTaken += " ";
       timeTaken += aTimeSpan.Minutes + " minute" + ( aTimeSpan.Minutes > 1 ? "s" : "" );
    }

    if( aTimeSpan.Seconds > 0 )
    {
       if( !string.IsNullOrEmpty( timeTaken ) )
           timeTaken += " ";
       timeTaken += aTimeSpan.Seconds + " second" + ( aTimeSpan.Seconds > 1 ? "s" : "" );
    }

    if( string.IsNullOrEmpty( timeTaken ) )
        timeTaken = "0 seconds.";

     return timeTaken;
}

答案 4 :(得分:1)

我喜欢约翰正在研究的答案。这就是我想出来的。

Convert.ToDateTime(t.ToString()).ToString("h \"Hour(s)\" m \"Minute(s)\" s \"Second(s)\"");

不考虑天数,因此如果需要,您需要添加。