2011-10-06 33 views
1

这是我对日期时间日历 中的文本框的输入,并将日期时间存储在数据库中我希望将值更改为日期时间。在asp.net中将字符串转换为日期时间

DateTime Res_date = Convert.ToDateTime(txt_RespondBy.Text); 

当我转换它给出一个错误说,字符串不被认为是有效的日期时间。如何将输入转换为日期时间.. txt_RespondBy.Text的

值:10/27/2011 03:25

+0

'txt_RespondBy.Text'的值是什么? –

+0

http://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy/2193037#2193037 –

回答

2

使用DateTime.Parse,请参阅MSDN:DateTime.Parse Method (String)

string[] dateStrings = {"2008-05-01T07:34:42-5:00", 
         "2008-05-01 7:34:42Z", 
         "Thu, 01 May 2008 07:34:42 GMT"}; 
foreach (string dateString in dateStrings) 
{ 
    DateTime convertedDate = DateTime.Parse(dateString); 
    Console.WriteLine("Converted {0} to {1} time {2}.", 
        dateString, 
        convertedDate.Kind.ToString(), 
        convertedDate); 
} 

也有ParseExact在那里你可以指定要使用的模式字符串....

相关问题