2010-03-17 31 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");