简体   繁体   English

字符串未被识别为有效的DateTime

[英]String was not recognized as a valid DateTime

I know such questions are in ton in SO but my situation seems little weird to me. 我知道这样的问题非常重要,但是我的情况对我来说似乎并不奇怪。

I have a textbox with a calendar extender control on my aspx page 我的aspx页面上有一个带有日历扩展控件的textbox

Default format is "d" in extenders date format property. 扩展程序日期格式属性中的默认格式为“ d”。 When I choose my date say 15th May 2012 from the calendar,it gives me 5/15/2012 , which is fine. 当我从日历中选择日期为15th May 2012 ,可以给我5/15/2012 ,这很好。

Since its a string and my db field is oftype datetime, so I am using 由于它是一个字符串,并且我的db字段的类型为datetime,所以我正在使用

Convert.ToDateTime(TextBox.Text);   // TextBox.Text = 5/15/2012

But it throws the exception, string was not recognized as valid datetime . 但是它引发了异常, string was not recognized as valid datetime

I then Change the code and used DateTime.Parse() but the issue remains. 然后,我更改代码并使用DateTime.Parse()但问题仍然存在。 Then i tried to reformat the date something like this, 然后我试图重新格式化这样的日期,

Convert.ToDateTime(string.Format("0:MM-dd-yyyy",TextBox.Text)).Date

but still its throwing exceptions.. 但仍然是抛出异常。

Please help me. 请帮我。

Use the following, 使用以下内容,

DateTime dt = DateTime.ParseExact(TextBox.Text, "dd/MM/yyyy", 
                                  CultureInfo.InvariantCulture);

There's probably a difference between your system's DateTime format and the DateTiem format the extender uses. 系统的DateTime格式与扩展程序使用的DateTiem格式之间可能存在差异。

I suppose that your dev machine date-time format is not equal to MM/DD/YYYY, but something else (for example DD/MM/YYYY). 您的开发机日期时间格式不等于MM / DD / YYYY,而是其他(例如DD / MM / YYYY)。 Have a look on your computer Regional settings to see your system date time format. 在计算机上查看“区域设置”以查看系统日期时间格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM