[英]C# Asp.net Convert.toDateTime(datevalue) gives error with different cultureinfo need datepattern look like 5/1/2012 12:00:00 AM
In my Asp.net Website i have two textboxes (txtstartdate, txtenddate) applied JQquery Datepicker which popups gives date in format (" 05/24/2012 "). 在我的Asp.net网站上,我有两个应用了JQquery Datepicker的 文本框 (txtstartdate,txtenddate),弹出框以格式(“ 05/24/2012 ”)给出日期。 Work fine for cultureInfo(" en-US "), when i change to (" de ") ie German its give error "System.FormatException: String was not recognized as a valid DateTime."
当我更改为(“ de ”)(即德语)时,它对cultureInfo(“ en-US ”)的工作正常,它给出了错误“ System.FormatException:字符串未被识别为有效的DateTime”。 .
。 In my code behind file i am writing this code
在文件后面的代码中,我正在编写此代码
string sDate = txtstartdate.Text; // 05/01/2012 (debugging gives this values)
string eDate = txtenddate.Text; // 05/24/2012 (debugging gives this values)
DateTime startdate = Convert.ToDateTime(sDate); // 5/1/2012 12:00:00 AM
DateTime enddate = Convert.ToDateTime(eDate); // 5/24/2012 12:00:00 AM
My requirement is datetime variable must give date of format 5/1/2012 12:00:00 AM whatever be the cultureinfo set doesnt matter to the date.So that i can execute select query in MsSql Server existing table having column datatype Datetime with data (5/1/2012 12:00:00 AM) formart 我的要求是datetime变量必须提供格式为5/1/2012 12:00:00 AM的日期,无论cultureinfo设置与date无关紧要,以便我可以在具有列数据类型为Datetime的数据的MsSql Server现有表中执行选择查询 (5/1/2012 12:00:00 AM)formart
After Changing the CultureInfo (from Masterpage dropdownlist) Englsih to German gives error Have tried this but not working 更改CultureInfo (从“母版页”下拉列表中)后, Englsih变为德语给出了错误尝试了此方法但不起作用
DateTime startdate = DateTime.ParseExact(sDate, "M/d/yyyy", null);
//tried also "MM/dd/yyyy"
Note: Set any cultileinfo, but the datetime pattern alwayz be 5/1/2012 12:00:00 AM ie en-US
culture 注意:设置任何cultileinfo,但日期时间模式始终为2012年5月1日上午12:00:00,即
en-US
文化
You could try using "MM/dd/yyyy"
as your format string when you call ParseExact
, assuming that you're sure the input will always be in that format. 您可以在调用
ParseExact
时尝试使用"MM/dd/yyyy"
作为格式字符串,假设您确定输入将始终采用该格式。
Alternatively, try using Convert.ToDateTime(yourString, CultureInfo.InvariantCulture)
instead. 或者,尝试改用
Convert.ToDateTime(yourString, CultureInfo.InvariantCulture)
。
I think in this case you have to set the altFormat of your calendar to the format used by SQL server. 我认为在这种情况下,您必须将日历的altFormat设置为SQL Server使用的格式。 Then, you can use a hidden field to store the altDate for each of your date pickers.
然后,您可以使用隐藏字段存储每个日期选择器的altDate。 When you submit, the value of the altField , in the correct format would be sent to your server instead of the culture-specific value that's displayed to the user.
提交时, 将以正确的格式将altField的值发送到您的服务器,而不是显示给用户的特定于区域性的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.