时间跨度格式

发布于 2024-07-20 01:35:21 字数 395 浏览 4 评论 0原文

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

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

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

重复 如何对 TimeSpan 进行 String.Format .NET 中具有自定义格式的对象?

How do you elegantly format a timespan to say example "1 hour 10 minutes" when you have declared it as :

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

?

I am of course aware that you could do some simple maths for this, but I was kinda hoping that there is something in .NET to handle this for me - for more complicated scenarios

Duplicate of How can I String.Format a TimeSpan object with a custom format in .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谢绝鈎搭 2024-07-27 01:35:22
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";
}
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";
}
遥远的绿洲 2024-07-27 01:35:22

从这里复制了我自己的答案:如何如何将 TimeSpan 转换为格式化字符串?

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;
}

Copied my own answer from here: 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;
}
丿*梦醉红颜 2024-07-27 01:35:22
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;
}
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;
}
ゃ懵逼小萝莉 2024-07-27 01:35:22

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

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

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

I like the answer John is working on. Here's what I came up with.

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

Doesn't account for days so you'd need to add that if you want it.

帅气称霸 2024-07-27 01:35: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")

There is no built-in functionality for this, you'll need to use a custom method, something like:

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文