仅生产中的日期时间错误
我的 asp.net 页面上有两个文本框,它们从日历控件中获取日期:
在日历选择上,我将所选必须的值分配给隐藏变量。 下面是代码:
DateTime dtOne;
dtOne = Convert.ToDateTime(hdnOne.Value.Trim().ToString());
DateTime dtTwo = Convert.ToDateTime(hdnTwo.Value.Trim().ToString());
当保留断点时,我得到 dtOne 的这个值
2011年9月2日 02:03...
它在预产品部署站点中工作正常,但是当我将相同的部署代码复制粘贴到生产中时,我收到以下错误:
System.FormatException: String was not recognized as a valid DateTime.
有什么建议为什么会发生以及如何解决它吗?
I have two textbox on my asp.net page which are taking dates from calendar control:
On Calendar selection, I am assigning the value of selected have to hidden variable.
below is the code:
DateTime dtOne;
dtOne = Convert.ToDateTime(hdnOne.Value.Trim().ToString());
DateTime dtTwo = Convert.ToDateTime(hdnTwo.Value.Trim().ToString());
When keeping breakpoint I get this value for dtOne
9/2/2011 02:03...
It is working fine in pre prod deployed site, but when I copy paste the same deployed code to the production I am getting below error:
System.FormatException: String was not recognized as a valid DateTime.
Any suggestion why it happened and how to resolve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的服务器上的文化设置很可能与您的开发计算机上的不同。在这种情况下,我认为 DateSeperator 是罪魁祸首。
您是否比较过两种环境下 hdnOne.Value.Trim().ToString() 的值?
The Culture settings on your server are most likely different from the one your development machine. In this case I would think the DateSeperator is the culprit.
Have you compared the value of hdnOne.Value.Trim().ToString() on both enviromnents?
是的 - 这是一个区域设置问题。本质上,您在 Convert.ToDateTime 中使用的格式字符串无法解析生产服务器返回的日期时间。因此,要么更改生产服务器区域设置,要么如果这令人不愉快(并且可能是),请在方法参数中使用适当的 IFormatProvider 参数来解释返回的字符串实际上采用哪种日期时间格式,以便它可以解析它。
此处的信息:http://msdn.microsoft.com/en-我们/library/system.convert.todatetime.aspx
Yep - this is a Regional Settings problem. Essentially, the format string you're using in Convert.ToDateTime isn't able to parse the datetime returned by the Production server. So either change your Production server Regional Settings, or if this is unpalatable (and it might be) use an appropriate IFormatProvider parameter in the method parameters to explain which datetime format the returned string is actually in so that it can parse it.
Info here: http://msdn.microsoft.com/en-us/library/system.convert.todatetime.aspx
您的生产环境和测试环境之间有什么区别 - 特别是在文化/区域设置方面? - 也许尝试显式匹配您的 javascript 日历控件和代码的解析所使用的文化。
所有内容都会引发此异常吗?或者只是一些用户内容?如果您使用的是文本框,那么这可能只是某些用户(或某些区域设置中的用户)输入了意外内容。
不管当前的修复如何,一旦真正的用户接触到您的网页,您就需要在运行时预料到此错误 - 因此要么处理 FormatException 要么使用 TryParse
Is there any difference between your production and testing environments - especially with regards to Culture/Locale? - maybe try explicitly matching the Culture used by both your javascript calendar control and your code's Parse.
Is this exception being thrown with all content? Or just some user content? If you are using TextBox's then this could be just some users (or users in some locales) entering unexpected content.
Regardless of this current fix, you will need to expect this error at runtime once real users get their hands on your web pages - so either handle the FormatException or use TryParse
如果文本框处于只读模式,那么当您从文本框中获取值时,您将得到空白。也使用 datetime.tryparse 而不是转换
if textbox is in readonly mode, then you will get blank when you will fetch the value from textbox. also use datetime.tryparse instead of convert