2010-03-17 40 views
0

我有这样的代码:字符串/日期转换问题(asp.net VB)

Dim birthdaystring As String = MonthBirth.SelectedValue.ToString & "/" & DayBirth.SelectedValue.ToString & "/" & YearBirth.SelectedValue.ToString 
Dim birthday As DateTime = Convert.ToDateTime(birthdaystring) 

将会产生错误(字符串未被识别为有效的DateTime)

字符串为“01/1963分之31“ 。

任何援助将不胜感激。

谢谢。

回答

2

的问题是可能是用于解析日期的文化需要一个MM/dd/yyyy格式,而不是dd/MM/yyyy格式。

而不是创建一个字符串和解析它的直接创造价值DateTime

Dim birthday As New DateTime(YearBirth.SelectedValue, MonthBirth.SelectedValue, DayBirth.SelectedValue) 
1

而不是创建一个字符串,您稍后尝试解析到一个日期试试这个:

Dim birthday As DateTime = new DateTime(_ 
    CType(YearBirth.SelectedValue, Integer), _ 
    CType(MonthBirth.SelectedValue, Integer), _ 
    CType(DayBirth.SelectedValue, Integer)) 
1

尝试将其更改为:

DateTime.Parse(birthdaystring); 

我猜它会的工作,但如果没有 - 您可以在解析中添加第二个参数,说明您输入的格式,即:

DateTime.Parse(birthdaystring,"MM/dd/yyyy");