日期时间格式异常错误
DateTime datuMDokumenta = Convert.ToDateTime(txtDatumDokum.Text);
txtDatumDokum.Text 就像“09.09.2011”。
但我收到 FormatException 错误。我必须解析日期吗?
DateTime datuMDokumenta = Convert.ToDateTime(txtDatumDokum.Text);
txtDatumDokum.Text is like "09.09.2011".
but i get FormatException error. Must i parse date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试使用 dd.MM.yyyy 格式字符串的 DateTime.ParseExact
Try DateTime.ParseExact with the dd.MM.yyyy format string
看起来不太好,无论如何尝试一下:
It's not good to see, anyway try this:
您需要告诉我们为什么文本输入使用这种格式。如果是因为用户以这种方式输入,那么您需要确保格式与
Thread.CurrentCulture.DateTimeFormat.ShortDatePattern
。改变文化(通过设置Thread.CurrentCulture
) 到适当的值将解决您的问题。如果您应该解析输入,无论它是什么格式,那么您将需要首先进行一些手动处理(也许使用
string.Replace
从输入中删除空格和其他分隔符)并且然后尝试使用DateTime.ParseExact
和已知的格式字符串。但这完全取决于为什么输入具有该格式,以及为什么应用程序的当前区域性与它不匹配。
You need to tell us why the text input is using this format. If it is because the user enters it this way, then you need to make sure that the format matches that given by
Thread.CurrentCulture.DateTimeFormat.ShortDatePattern
. Changing the culture (by settingThread.CurrentCulture
) to an appropriate value will then solve your problem.If you are supposed to parse the input no matter what format it is in, then you will need to do some manual processing first (perhaps remove spaces and other delimiter characters from the input with
string.Replace
) and then try to parse the date usingDateTime.ParseExact
and a known format string.But it all depends on why the input has that format, and why your application's current culture does not match it.
您可以尝试这个,TryParse 避免解析异常。然后您只需要检查结果以确保它已解析。
您必须确定这是否是适合您的应用程序的良好解决方案。
看这个例子:
http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx
根据您提供的日期判断,您需要包含一种文化,de-DE 接受 01.01.11 类型的日期,但我不确定您实际想要使用哪一个,您需要来决定......代码将如下所示:
可以在此处找到文化列表,选择适合您的文化:
http://msdn.microsoft .com/en-us/library/system.globalization.cultureinfo%28v=vs.71%29.aspx
这里的优点是这段代码工作量更大,但很难破解。假设您在 TextBox 上使用自由文本条目,您不希望引发异常。
You could try this, TryParse avoids parsing exceptions.. Then you just need check result to be sure that it parsed.
You will have to determine if this is a good solution for your application.
See this example:
http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx
Judging by the date you gave you need to include a culture, de-DE accepts 01.01.11 type of dates but I'm not sure which one you actually want to use, you'll need to decide that.. the Code would look like this:
A list of cultures can be found here, select the appropriate one for you:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.71%29.aspx
The plus here is that this code is a bit more work but it is very difficult to break. Assuming you are using a free text entry on a TextBox you don't want to be throwing exceptions.
是的,您必须解析当前文化中的输入日期。
Yes you have to parse input date in current culture.
DateTime dt = Convert.ToDateTime(txtDatumDokum.Text)
是的...没有问题
DateTime dt = Convert.ToDateTime(txtDatumDokum.Text)
It is right...there is no isssue
在紧凑框架 3.5 下的反序列化调用期间,我之前遇到过一些意外行为。
我已从使用 OpenNETCF 序列化类转换为使用框架 XML 序列化类。这样做时,默认时间格式以及属性/公共成员的顺序都已更改。长话短说,我公开了一个文本属性,它将我的日期时间转换回我的 VB6 应用程序期望的格式。
Date.blah 不起作用。 dumbDate.blah 有效。奇怪的。
During a Deserialization call under compact framework 3.5 i've had some unexpected behaviour before.
I've converted from using the OpenNETCF serialization classes to the framework XML serialization class. In doing so, the default time format has changed and the order of property/public members. So long story short, i've exposed a text property which converts my date-times back to the format my VB6 application is expecting.
Date.blah doesn't work. dumbDate.blah works. strange.
您的代码:
尝试将其更改为:
当您打印
日期/时间
时打印datuMDokumenta.Text
your code:
try changing this to:
and when u print the
date/time
print datuMDokumenta.Text