TimeSpan 和 DateTime 的格式化字符串之间的不同行为

发布于 2024-09-13 00:42:56 字数 1079 浏览 3 评论 0原文

今天编码时,我注意到时间跨度和格式化字符串有些奇怪。我试图打印一个时间跨度,例如将 01:03:37 打印为 1:03:37 (没有前导 0 几个小时)。所以我使用了格式字符串h:mm:ss。然而,这给了我一个前导 0。如果我将 TimeSpan 转换为 DateTime 并再次执行相同的操作,h 格式化字符串将按我的预期工作。

示例控制台程序:

class Program
{
    static void Main(string[] args)
    {
        var time = new TimeSpan(01, 03, 37);

        var culture = new CultureInfo("sv-SE");

        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        Console.WriteLine(time.ToString());

        Console.WriteLine(string.Format(culture, "{0:h:mm:ss}", time));
        Console.WriteLine(string.Format(culture, "{0:hh:mm:ss}", time));

        Console.WriteLine((new DateTime(time.Ticks)).ToString("h:mm:ss", culture));
        Console.WriteLine((new DateTime(time.Ticks)).ToString("hh:mm:ss", culture));

        Console.ReadKey();
    }
}

输出:

01:03:37
01:03:37  // <-- expected: 1:03:37
01:03:37
1:03:37
01:03:37

为什么 TimeSpan 和 DateTime 的行为不同?

While coding today, I noticed something odd with timespans and formatting strings. I was trying to print a timespan, for instance 01:03:37 as 1:03:37 (without the leading 0 for hours). So I used the format string h:mm:ss. This, however, gave me a leading 0. If I converted the TimeSpan to a DateTime and did the same thing again, the h formatting string worked as I expected.

A sample console program:

class Program
{
    static void Main(string[] args)
    {
        var time = new TimeSpan(01, 03, 37);

        var culture = new CultureInfo("sv-SE");

        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        Console.WriteLine(time.ToString());

        Console.WriteLine(string.Format(culture, "{0:h:mm:ss}", time));
        Console.WriteLine(string.Format(culture, "{0:hh:mm:ss}", time));

        Console.WriteLine((new DateTime(time.Ticks)).ToString("h:mm:ss", culture));
        Console.WriteLine((new DateTime(time.Ticks)).ToString("hh:mm:ss", culture));

        Console.ReadKey();
    }
}

Output:

01:03:37
01:03:37  // <-- expected: 1:03:37
01:03:37
1:03:37
01:03:37

Why is the TimeSpan and DateTime behaving differently?

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

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

发布评论

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

评论(3

蓦然回首 2024-09-20 00:42:56

因为您的格式字符串不适用于 TimeSpan 并且 TimeSpan.ToString() 始终返回 (来自 MSDN):

表示值的字符串
这个例子。返回值为
形式:

[-][d.]时:分:秒[.ff]

Because your formatting string do not work for TimeSpan and TimeSpan.ToString() always returns (from MSDN):

A string that represents the value of
this instance. The return value is of
the form:

[-][d.]hh:mm:ss[.ff]

安静被遗忘 2024-09-20 00:42:56

在 .Net 4.0 之前,TimeSpans 不支持格式字符串。

在 .Net 4.0 中,格式字符串已记录

Until .Net 4.0, TimeSpans do not support format strings.

In .Net 4.0, the format strings are documented.

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