为什么这个特定的 TimeSpan 格式字符串在 .NET 4 中停止工作?
考虑这段代码(带有示例):
DateTime dt1 = DateTime.Parse("7/30/2010 9:33:29.1234567 AM");
DateTime dt2 = DateTime.Parse("6/30/2010 9:33:00.7654321 AM");
TimeSpan ts = dt1 - dt2;
Console.WriteLine(string.Format( "{0:d.hh:mm:ss.ff}", ts ));
这是我至少从 .NET 1.1 以来一直在工作的一段代码的代表。
它在 1.1 到 3.5 中运行良好,输出如下(对于这些虚拟输入):
30.00:00:28.3580246
但现在我看到它在 .NET 4 中终止并显示错误消息:
输入字符串格式不正确。< /code>
因此,就好像 .NET 4 突然决定不喜欢这种时差格式。将行更改为,比方说
Console.WriteLine(string.Format( "{0}", ts.ToString("d.hh:mm:ss.ff") ));
具有相同的效果。
现在我注意到的是,如果我只执行默认的 .ToString()
我会得到相同的输出。我相信这个思考过程是,这是针对未来版本中默认格式更改的保险政策。但现在看来这甚至不是一个选择。
有谁知道为什么会发生这种变化,以及我是否做错了什么,或者是否有最佳实践方法来完成我想要完成的任务?
Consider this code (prestuffed with an example):
DateTime dt1 = DateTime.Parse("7/30/2010 9:33:29.1234567 AM");
DateTime dt2 = DateTime.Parse("6/30/2010 9:33:00.7654321 AM");
TimeSpan ts = dt1 - dt2;
Console.WriteLine(string.Format( "{0:d.hh:mm:ss.ff}", ts ));
This is representative of a piece of code that I've had working since .NET 1.1 at least.
It worked fine in 1.1 through 3.5 with the following output (for these dummied up inputs):
30.00:00:28.3580246
But now I'm seeing that it dies in .NET 4 with the error message:
Input string was not in a correct format.
So it's as if .NET 4 has suddenly decided it doesn't like this format for time differences. Changing the line to, say
Console.WriteLine(string.Format( "{0}", ts.ToString("d.hh:mm:ss.ff") ));
has the same effect.
Now the thing I've noticed is that if I just do the default .ToString()
I get the same output. I believe the thought process was that this was an insurance policy against the default format changing in a future version. But now it doesn't look like that's even an option.
Does anyone know why this changed and if I'm doing something wrong or if there's a best practices way to do what I'm trying to accomplish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个配置开关来恢复的旧行为="http://msdn.microsoft.com/en-us/library/system.timespan.aspx" rel="nofollow noreferrer">时间跨度。
There is a configuration switch to restore the old behaviour of TimeSpan.
配置开关的替代方案是与以前版本兼容的格式更改。
此解决方案的详细信息此处< /a>.
An alternative to the configuration switch is a format change that is compatible with previous versions.
This solution is detail here.
事实上,您在代码中使用的复合格式字符串根本没有任何效果,因为
TimeSpan
不支持自定义格式字符串(.NET < 4.0)。ie 无论格式字符串如何,您的 TimeSpan 的格式始终如
30.00:00:28.3580246
。来自 MSDN:
In fact, the composite format string you've been using in your code did not have any effect at all, because
TimeSpan
does not support custom format strings (.NET < 4.0).i.e. Your TimeSpan would have always been formatted like
30.00:00:28.3580246
regardless of format string.From MSDN:
正如 Mitch Wheat 和 Saeb Amini 在 他们的 答案,
TimeSpan
未实现 .NET 4.0 之前的IFormattable
。因此,格式字符串对TimeSpan.ToString()
输出没有影响,因为它们被忽略了。也就是说,如果您希望在 .NET 框架的所有版本中格式化
TimeSpan
值,最好将TimeSpan
值转换为DateTime
并然后格式化结果,如下所示:As indicated by Mitch Wheat and Saeb Amini in their answers,
TimeSpan
does not implementIFormattable
prior to .NET 4.0. Consequently, format strings have no effect on theTimeSpan.ToString()
output since they were ignored.That said, fi you desire to format a
TimeSpan
value across all versions of the .NET framework, it is much better to convert theTimeSpan
value toDateTime
and then format that result as shown below:我已经粘贴了你的一段代码,它似乎是一个文化问题:
对于.NET 2,也会引发 FormatException
如果我指定了美国文化(默认情况下文化是 fr-FR),代码可以工作:
你也可以指定不变的文化忽略文化
I've pasted your piece of code and it seems to be a culture problem :
with .NET 2 an FormatException is thrown too
If I specified the us culture (culture is fr-FR by default) , the code works :
You can also specified an Invariant culture to ignore the culture