2014-03-06 99 views
-1

在我的應用我想從字符串日期格式的日期轉換時的錯誤:錯誤將字符串轉換爲自定義日期格式

dateFormat = Format(CDate("2014-mar-06"), "MM/dd/yyyy") 

錯誤

Conversion from string "2014-mar-06" to type 'Date' is not valid 

此問題只來當我的地區和語言設置是西班牙語(墨西哥)(或任何西班牙語,但不適用於其他人)在Windows 7中。什麼是問題以及如何解決這個問題?

回答

1

避免使用VB6功能,如CType,並使用.NET方法代替TryParse。 也CultureInfo.InvariantCulture得到CultureInfo對象,它是文化無關的(不變)

試試這個

Dim dateString = "2014-mar-06" 
    Dim dateValue As DateTime 

    If DateTime.TryParseExact(dateString, _ 
     "yyyy-MMM-dd", CultureInfo.InvariantCulture, _ 
     DateTimeStyles.None, dateValue) Then 

     Dim myDate = dateValue.ToString("MM/dd/yyyy") 'Your Date is stored in myDate 

    Else 
     'Unable to parse your dateString 
    End If 
相關問題