DateTime.ParseExact 格式错误
我使用文本框以 DD-MM-YYYY 格式存储日期,因为我使用 SQL Server,所以我曾经从文本框中获取值,然后将其放入 DateTime dt 类型的变量中,
DateTime dt = DateTime.ParseExact(TextBox10.Text,"MM-DD-YYYY",CultureInfo.InvariantCulture);
我收到格式规范错误,我浏览了很多文档,但仍然无法找到我出错的地方
,我正在使用 C# 和 ADO.net 请问有人可以纠正我吗?
I have used a textbox to store a date in DD-MM-YYYY format and since i use SQL server i used to get the value from the textbox and then place it in a variable of type DateTime dt
DateTime dt = DateTime.ParseExact(TextBox10.Text,"MM-DD-YYYY",CultureInfo.InvariantCulture);
I am getting format specification error, I went through many documentation but still couldn't get where i am going wrong
This i am using C# and ADO.net
Please can anyone correct me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来你把DD和MM搞混了。请尝试以下操作:
请注意,我将“DD”更改为“dd”!
更新:
好的,所以我将“YYYY”更改为“yyyy”并运行下面的代码并成功解析:
如果您的用户确实以“dd-MM-yyyy”格式传递日期,则此应该为你工作。请记住,第二个字符串与您存储在数据库中的任何格式无关。它只与 ParseExact 中第一个字符串参数的格式有关。 HTH。
It looks like you mixed up DD and MM. Try below:
Notice that I changed "DD" to "dd"!
UPDATE:
Ok, so I changed "YYYY" to "yyyy" and ran the code below and it parsed sucessfully:
If your users really are passing in dates in the format "dd-MM-yyyy" this should work for you. Keep in mind that the second string has nothing to do with whatever format you are storing in your database. It only has to do with the format of the first string argument in ParseExact. HTH.
试试这个
注意日期格式字符串的大小写。 “dd”可以与“DD”不同
try this
Take care of date format string casing. "dd" can be different from "DD"
如上所述,日期格式字符串区分大小写。
但是,您确定要使用 DateTime.ParseExact 而不是 DateTime.Parse 吗?
As mentioned above, date format string is case sensitive.
However, are you sure you want to use DateTime.ParseExact as opposed to DateTime.Parse?