如何以文化意识方式格式化TimeSpan的HH:mm:ss分隔符? [英] How to format the HH:mm:ss separators of a TimeSpan in a culture-aware manner?

查看:93
本文介绍了如何以文化意识方式格式化TimeSpan的HH:mm:ss分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在世界许多国家都可以看到的应用程序。除了分隔符以外,没有多少国家显示小时,分钟和秒,但作为分隔符,有几个国家,我想确保时间格式正确无误。 DateTime在这方面很棒,但TimeSpan却不是。这些摘录来自我使用.Net 4在Visual Studio 2010中的即时窗口中,我的区域设置为Malayalam(印度)。 dateTime.Now调用还反映了如何在我的时钟,Microsoft Outlook和其他区域上显示时间。

I'm working on an app that may be seen in many countries in the world. There are not many countries that show hours, minutes and seconds with something other than : as a seperator but there are a few and I want to make sure that times are formatted correctly for their region. DateTime is great at this but TimeSpan isn't. These snippets are from my immediate Window in Visual Studio 2010 using .Net 4 with my region set to Malayalam (India). The dateTime.Now call also reflects how time is shown on my clock, Microsoft Outlook and other areas.

DateTime.Now.ToString()
"02-10-12 17.00.58"

http://msdn.microsoft.com/en-us/library/dd784379.aspx 说如果formatProvider为null,则使用与当前区域性关联的DateTimeFormatInfo对象。如果format是自定义格式字符串,则将忽略formatProvider参数。可以认为,我什至不需要通过Current CultureInfo。我想要的格式是hh.mm.ss,但是在大多数其他语言中却是hh:mm:ss,如果还有其他可能性,它也应该会自动反映出来-基本上,TimeSpan 应该文化意识,就像DateTime一样。

http://msdn.microsoft.com/en-us/library/dd784379.aspx Says "If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used. If format is a custom format string, the formatProvider parameter is ignored." It stands to reason then that i shouldn't need to even pass in the Current CultureInfo. The format i want here is hh.mm.ss but obvioulsy hh:mm:ss when in most other languages, and if there are anoy other poossibilities, it should automatically reflect those too - basically TimeSpan should be culture-Aware, just as DateTime is.

但是:

timeRemaining.ToString()
"00:02:09"
timeRemaining.ToString("c")
"00:02:09"
timeRemaining.ToString("c", CultureInfo.CurrentCulture)
"00:02:09"
timeRemaining.ToString("g")
"0:02:09"
timeRemaining.ToString("G")
"0:00:02:09.0000000"
timeRemaining.ToString("t")
"00:02:09"
timeRemaining.ToString("g", CultureInfo.CurrentCulture)
"0:02:09"
timeRemaining.ToString("g", CultureInfo.CurrentUICulture)
"0:02:09"
timeRemaining.ToString("G", CultureInfo.CurrentUICulture)
"0:00:02:09.0000000"
timeRemaining.ToString("G", CultureInfo.CurrentCulture)
"0:00:02:09.0000000"
timeRemaining.ToString("t", CultureInfo.CurrentCulture)
"00:02:09"

我正在寻找一个简单的方法,一行以文化意识方式输出timeSpan。任何想法都会受到赞赏。

I'm looking for a simple, single line to output a timeSpan in a culture-Aware manner. Any ideas are appreciated.

推荐答案

看起来像个错误,您可以在connect.microsoft.com上进行报告。同时,一种解决方法是利用DateTime格式。像这样:

Looks like a bug, you can report it at connect.microsoft.com. Meanwhile, a workaround is to take advantage of DateTime formatting. Like this:

using System;
using System.Globalization;

class Program {
    static void Main(string[] args) {
        var ci = CultureInfo.GetCultureInfo("ml-IN");
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;
        var ts = new TimeSpan(0, 2, 9);
        var dt = new DateTime(Math.Abs(ts.Ticks));
        Console.WriteLine(dt.ToString("HH:mm:ss"));
        Console.ReadLine();
    }
}

输出:


00.02.09

00.02.09

这篇关于如何以文化意识方式格式化TimeSpan的HH:mm:ss分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆