【问题标题】:Timespan formatting [duplicate]时间跨度格式[重复]
【发布时间】:2010-10-24 19:37:00
【问题描述】:

当您将时间跨度声明为:

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

?

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

重复 How can I String.Format a TimeSpan object with a custom format in .NET?

【问题讨论】:

    标签: c# .net timespan


    【解决方案1】:

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

    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 小时,所以不太会这样做。我想我只需要做一些 iffing :) Ty 反正
    • @jalchr:复数对于零值是正确的,所以将其保留为== 1。例如0 小时 0 分钟是正确的。
    【解决方案2】:
    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";
    }
    

    【讨论】:

    • 我只是在自己的代码中做的,简单的在参数前加一个“this”。
    • 我希望俄语的复数形式和英语一样简单 =)
    • @MaximV.Pavlov +1 搞笑评论 >;-) 我想看到一个俄语复数词,它可以在“with 2345678 Girls”这句话中正确地写出“2345678”。实际上,我还没有遇到一个可以毫不犹豫地做到这一点的人。
    【解决方案3】:

    从这里复制我自己的答案: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;
    }
    

    【讨论】:

      【解决方案4】:
      公共静态字符串 GetDurationInWords( TimeSpan aTimeSpan ) { 字符串 timeTaken = string.Empty; 如果(aTimeSpan.Days > 0) timeTaken += aTimeSpan.Days + "day" + (aTimeSpan.Days > 1 ? "s" : "" ); 如果(aTimeSpan.Hours > 0) { if(!string.IsNullOrEmpty(timeTaken)) 时间 += " "; timeTaken += aTimeSpan.Hours + "hour" + (aTimeSpan.Hours > 1 ? "s" : "" ); } 如果(aTimeSpan.Minutes > 0) { if(!string.IsNullOrEmpty(timeTaken)) 时间 += " "; timeTaken += aTimeSpan.Minutes + "minute" + (aTimeSpan.Minutes > 1 ? "s" : "" ); } 如果(aTimeSpan.Seconds > 0) { if(!string.IsNullOrEmpty(timeTaken)) 时间 += " "; timeTaken += aTimeSpan.Seconds + "秒" + (aTimeSpan.Seconds > 1 ? "s" : "" ); } if(string.IsNullOrEmpty(timeTaken)) timeTaken = "0 秒。"; 返回时间; }

      【讨论】:

      • 你的代码需要重构!
      【解决方案5】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-22
        • 2020-01-10
        相关资源
        最近更新 更多