TimeSpan“美好时光” C# 中的格式

发布于 2024-10-26 08:40:17 字数 1012 浏览 4 评论 0原文

输入此问题的标题使我看到此问题。我正在寻找同样的东西,但是如果你明白我的意思的话,可能是不太静态格式的东西?

我正在编写一个快速程序,它将获取两个 DateTime 对象的 TimeSpan 持续时间并将它们输出以打印到纸张上。

首选格式是:XX 天,YY 小时,ZZ 分钟(秒是无关紧要的;天也是如此,因为我预计时间跨度不会超过几个小时) .

说出以下代码(作为示例):

DateTime start = DateTime.Now;
DateTime end = DateTime.Now.AddMinutes(135);
TimeSpan duration = (end - start);

// output duration as XX hours, YY minutes

谢谢,
扎克

P.S. 我还遇到了“PrettyTime.NET”,但它输出了诸如“3天前”之类的内容等等。我在 google 上发现的用于格式化持续时间字符串的每个其他主题(如上面的示例)都引导我找到基于 Java 的解决方案。 :\


更新 2014-09-08:

为了 3 年后的跟进,我在 NuGet 和 Github 上找到了一个很棒的库,名为“Humanizer" 这可以解决我遇到的这个问题。我没怎么用过它,但我确实尝试过,而且效果很好。不过,它是一个相当大的库(它会向项目的构建路径发出大量不同文化的文件夹(我确信有一种方法可以自定义它))。

Typing in the title to this question brought me to this question. I'm looking for the same thing, but something perhaps less statically formatted if you get what I mean?

I'm writing a quick program that will be taking a TimeSpan duration of two DateTime objects and outputting them for printing to paper.

The format that would be preferred is: XX days, YY hours, ZZ minutes (seconds are irrelevant; as are days because I don't expect the timespan to incorporate more than a few hours).

Say the following code (as an example):

DateTime start = DateTime.Now;
DateTime end = DateTime.Now.AddMinutes(135);
TimeSpan duration = (end - start);

// output duration as XX hours, YY minutes

Thanks,
Zack

P.S.
I also ran across "PrettyTime.NET", but it output things like "3 days ago", etc. Every other topic I found on google to format time duration strings like the above examples lead me to a Java-based solution. :\


UPDATE 2014-09-08:

To follow-up after 3 years, I've found an awesome library on NuGet and Github called "Humanizer" which would solve this issue I was having. I haven't used it that much, but I did try it and it works beautifully. It's quite large of a library though (it emits a whole ton of folders for different cultures to your project's build path (I'm sure there's a way to to customize it)).

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

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

发布评论

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

评论(3

世界和平 2024-11-02 08:40:17

如果你关心多元化:

public static string ToPrettyFormat(this TimeSpan span) {

    if (span == TimeSpan.Zero) return "0 minutes";

    var sb = new StringBuilder();
    if (span.Days > 0)
        sb.AppendFormat("{0} day{1} ", span.Days, span.Days > 1 ? "s" : String.Empty);
    if (span.Hours > 0)
        sb.AppendFormat("{0} hour{1} ", span.Hours, span.Hours > 1 ? "s" : String.Empty);
    if (span.Minutes > 0)
        sb.AppendFormat("{0} minute{1} ", span.Minutes, span.Minutes > 1 ? "s" : String.Empty);
    return sb.ToString();

}

And if you care about pluralization:

public static string ToPrettyFormat(this TimeSpan span) {

    if (span == TimeSpan.Zero) return "0 minutes";

    var sb = new StringBuilder();
    if (span.Days > 0)
        sb.AppendFormat("{0} day{1} ", span.Days, span.Days > 1 ? "s" : String.Empty);
    if (span.Hours > 0)
        sb.AppendFormat("{0} hour{1} ", span.Hours, span.Hours > 1 ? "s" : String.Empty);
    if (span.Minutes > 0)
        sb.AppendFormat("{0} minute{1} ", span.Minutes, span.Minutes > 1 ? "s" : String.Empty);
    return sb.ToString();

}
酸甜透明夹心 2024-11-02 08:40:17

你可以直接输出:

 string result = string.Format("{0} days, {1} hours, {2} minutes", duration.Days, duration.Hours, duration.Minutes);

如果你要处理“短”时间,并且你希望它更干净,你可以这样做:

public string PrettyFormatTimeSpan(TimeSpan span)
{
    if (span.Days > 0)
         return string.Format("{0} days, {1} hours, {2} minutes", span.Days, span.Hours, span.Minutes);
    if (span.Hours > 0)
         return string.Format("{0} hours, {1} minutes", span.Hours, span.Minutes);

    return  string.Format("{0} minutes", span.Minutes);
}

You can just output this directly:

 string result = string.Format("{0} days, {1} hours, {2} minutes", duration.Days, duration.Hours, duration.Minutes);

If you are going to be handling "short" times, and you want this to be cleaner, you could do something like:

public string PrettyFormatTimeSpan(TimeSpan span)
{
    if (span.Days > 0)
         return string.Format("{0} days, {1} hours, {2} minutes", span.Days, span.Hours, span.Minutes);
    if (span.Hours > 0)
         return string.Format("{0} hours, {1} minutes", span.Hours, span.Minutes);

    return  string.Format("{0} minutes", span.Minutes);
}
酒儿 2024-11-02 08:40:17

使用 C# 7:

string FormatTimeSpan(TimeSpan timeSpan)
{
    string FormatPart(int quantity, string name) => quantity > 0 ? $"{quantity} {name}{(quantity > 1 ? "s" : "")}" : null;
    return string.Join(", ", new[] { FormatPart(timeSpan.Days, "day"), FormatPart(timeSpan.Hours, "hour"), FormatPart(timeSpan.Minutes, "minute") }.Where(x => x != null));
}

With C# 7:

string FormatTimeSpan(TimeSpan timeSpan)
{
    string FormatPart(int quantity, string name) => quantity > 0 ? $"{quantity} {name}{(quantity > 1 ? "s" : "")}" : null;
    return string.Join(", ", new[] { FormatPart(timeSpan.Days, "day"), FormatPart(timeSpan.Hours, "hour"), FormatPart(timeSpan.Minutes, "minute") }.Where(x => x != null));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文