将字符串转换(解析)为日期

如果你知道要转换(解析)的字符串的格式,你应该使用 DateTime.ParseExact

Dim dateString As String = "12.07.2003"
Dim dateFormat As String = "dd.MM.yyyy"
Dim dateValue As Date

dateValue = DateTime.ParseExact(dateString, dateFormat, Globalization.CultureInfo.InvariantCulture)

如果你不确定字符串的格式,可以使用 DateTime.TryParseExact 并测试结果以查看是否已解析:

Dim dateString As String = "23-09-2013"
Dim dateFormat As String = "dd-MM-yyyy"
Dim dateValue As Date

If DateTime.TryParseExact(dateString, dateFormat, Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, dateValue) Then
    'the parse worked and the dateValue variable now holds the datetime that was parsed as it is passing in ByRef
Else
    'the parse failed
End If