Hug*_*une 2 c# timespan localization cultureinfo .net-4.6
我想在本地文化中输出一个时间跨度,将其作为csv导入Excel中.时间跨度包含毫秒.
与英国文化,这意味着,例如00:00:01.2345678
与德国文化,这应该是00:00:01,2345678
(逗号而不是点)
但无论我为CultureInfo对象尝试哪种设置,我都无法让它工作:
TimeSpan t = new TimeSpan(12345678);
var cul = CultureInfo.GetCultureInfo("de");
// cul.DateTimeFormat.FullTimeSpanPositivePattern == "d':'h':'mm':'ss','FFFFFFF";
// (note the comma)
Console.WriteLine(String.Format(cul, "{0}", t));
// expected: "00:00:01,2345678" (with ,)
// actual: "00:00:01.2345678" (with .)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我甚至无法分辨CultureInfo类的哪些属性定义了这个.这是硬编码的吗?
我知道我可以明确地定义输出格式:`String.Format("{0:hh \:mm \:ss \,FFFFFFF}",t)
但有没有办法使用a IFormatProvider
,所以c#将使用给定的文化?
使用"g"-format说明符,所以t.ToString("g", culture)
.默认情况下,TimeSpan
使用"c"
-format说明符转换a ,这是一种常见的非文化特定格式.
用string.Format
这个就可以了
String.Format(cul, "{0:g}", t)
Run Code Online (Sandbox Code Playgroud)
有关格式化时间跨度的更多信息可以在文档中找到
归档时间: |
|
查看次数: |
258 次 |
最近记录: |