C# DateTime.Parse(myString) 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/162335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is wrong with DateTime.Parse(myString)?
提问by casademora
I was browsing Scott Hanselman's Developer Interview question list, and ran across this question:
我正在浏览 Scott Hanselman 的Developer Interview question list,并遇到了这个问题:
What is wrong with DateTime.Parse(myString)?
DateTime.Parse(myString) 有什么问题?
While I know there are inherent risks in parsing a string of unknow format or origin, are there other reasons? Is it to use DateTime.ParseExact instead? Should it be myString.ToString() first?
虽然我知道解析未知格式或来源的字符串存在固有风险,但还有其他原因吗?是否使用 DateTime.ParseExact 代替?应该先是 myString.ToString() 吗?
采纳答案by Joel Coehoorn
In addition the locale problem, DateTime.Parse()
could also throw an exception which you would then have to catch. Use DateTime.TryParse()
or DateTime.TryParseExact()
instead.
此外,语言环境问题DateTime.Parse()
还可能引发异常,然后您必须捕获该异常。使用DateTime.TryParse()
或DateTime.TryParseExact()
代替。
回答by Stephen Wrighton
this blog post explains it, but the general thing is that there's no cultureinfo associated with the parse.
这篇博客文章对此进行了解释,但一般而言,没有与解析相关的文化信息。
回答by Jon Skeet
Using the current thread culture on the system is often a bad idea, as is "try a variety of formats, and see if any of them work."
在系统上使用当前的线程文化通常是一个坏主意,因为“尝试各种格式,看看它们中的任何一个是否有效”。
ParseExact with a specific culture is a much more controlled and precise approach. (Even if you specify the current culture, it makes it more obvious to readers that that's what's going on.)
具有特定文化的 ParseExact 是一种更加可控和精确的方法。(即使您指定了当前的文化,它也会让读者更清楚地了解正在发生的事情。)
回答by Torbj?rn Gyllebring
As MSDN Puts it:
正如 MSDN 所说:
Because the Parse(String) method tries to parse the string representation of a date and time using the formatting rules of the current culture, trying to parse a particular string across different cultures can either fail or return different results. If a specific date and time format will be parsed across different locales, use the DateTime.Parse(String, IFormatProvider) method or one of the overloads of the ParseExact method and provide a format specifier.
由于 Parse(String) 方法尝试使用当前区域性的格式规则解析日期和时间的字符串表示,因此尝试跨不同区域性解析特定字符串可能会失败或返回不同的结果。如果将跨不同区域设置解析特定日期和时间格式,请使用 DateTime.Parse(String, IFormatProvider) 方法或 ParseExact 方法的重载之一并提供格式说明符。
回答by Torbj?rn
In addition to unknown user input the environment may be unknown.., so I guess that even if you control the input format, what the parse expect may be different..
除了未知的用户输入环境可能是未知的..,所以我猜即使你控制了输入格式,解析期望的内容可能会有所不同..
回答by Michael Brown
That question is just to see if the developer knows the issues with that. First you should use TryParse because Parse throws an exception if it's unparseable. Also it doesn't take locale into account so in a web scenario, if a british User types 02/10/2008, and my server is using an en-US locale, I get February 10,2008 instead of October 2, 2008.
这个问题只是为了看看开发人员是否知道问题所在。首先,您应该使用 TryParse,因为 Parse 会在无法解析时抛出异常。此外,它不考虑语言环境,因此在 Web 场景中,如果英国用户键入 02/10/2008,并且我的服务器使用 en-US 语言环境,我会得到 2008 年 2 月 10 日而不是 2008 年 10 月 2 日。
There might be other issues but those are the first two that sprung to mind.
可能还有其他问题,但这些是首先想到的两个问题。
回答by John Rudy
My gut reaction is going to be that you hit it with unknown formats/origins. There may be other reasons -- for example, from that single line, do we knowthat myString
is a string? (I'm assuming it is, of course.)
我的直觉反应是你用未知的格式/来源击中它。可能还有其他原因——例如,从那一行中,我们知道这myString
是一个字符串吗?(我假设它是,当然。)
Generally I recommend the TryParse method instead. It's slightly more verbose, but helps prevent exceptions -- as long as your code behaves appropriately in the case of invalid input.
通常我推荐使用 TryParse 方法。它稍微冗长一些,但有助于防止异常——只要您的代码在无效输入的情况下表现得当。
Of course, based on your wording to this ... I assume you already knew all that. :)
当然,根据你的措辞......我假设你已经知道这一切。:)
回答by Andy C
The answer depends on the code which surrounds DateTime.Parse(myString) and the requirements of the parse
答案取决于围绕 DateTime.Parse(myString) 的代码和解析的要求
You may already have check the string is a valid format based on a regex and you may not be interesting in Culture Information. You may also know that the data comes from a known file format with a known date convention so throwing an exception may be exactly what is desired.
您可能已经根据正则表达式检查了字符串是否为有效格式,并且您可能对文化信息不感兴趣。您可能还知道数据来自具有已知日期约定的已知文件格式,因此抛出异常可能正是所期望的。
Without context the question is quite ambiguous and the real answer to it has to be "it depends on the context where the code is used as to what is wrong with it, if an
没有上下文,这个问题是相当模糊的,它的真正答案必须是“这取决于使用代码的上下文,如果它有什么问题,