因为您不打算将日期字符串传递给 SQL(ARE YOU?),您不妨解析日期以确保有效性:
Imports System.Globalization
Module Module1
' Take a string of year or year and month or year and month and day and
' convert it to a DateTime, defaulting to month=1 or day=1 if they are missing
' Throw an exception if this is not possible.
Function MakeExplicitDate(s As String) As DateTime
Dim cul = New CultureInfo("en-GB")
Dim dt As DateTime
If Not DateTime.TryParse(s, cul, Nothing, dt) Then
s &= "-01"
If Not DateTime.TryParse(s, cul, Nothing, dt) Then
s &= "-01"
If Not DateTime.TryParse(s, cul, Nothing, dt) Then
Throw New Exception("Could not parse date.")
End If
End If
End If
Return dt
End Function
Sub Main()
Dim s = "P:2009 to 2011.02.13"
If s.StartsWith("P:") Then
s = s.Substring(2, Math.Max(0, s.Length - 2))
Dim dates = s.ToLowerInvariant.Split({"to"}, StringSplitOptions.RemoveEmptyEntries)
If dates.Count > 2 Then
Throw New Exception("Too many dates supplied.")
End If
Dim datesToUse As New List(Of DateTime)
For i = 0 To dates.Count - 1
datesToUse.Add(MakeExplicitDate(dates(i)))
Next
For Each dtu In datesToUse
Console.WriteLine(dtu.ToString("yyyy-MM-dd"))
Next
End If
Console.ReadLine()
End Sub
End Module