在winform.net中分析日期格式的日期字符串
我正在使用vb.net、framework 3.5和winform应用程序。我正在从包含日期作为文本字符串的数据库加载记录:在winform.net中分析日期格式的日期字符串,.net,vb.net,winforms,.net,Vb.net,Winforms,我正在使用vb.net、framework 3.5和winform应用程序。我正在从包含日期作为文本字符串的数据库加载记录: 4/5/2016 (d/M/yyyy) ' 4th May 2016 06/05/2016 (dd/MM/yyyy) ' 6th May 2016 05/8/2016 (dd/M/yyyy) ' 5th August 2016 6/08/2016 (d/MM/yyyy) ' 6th August 2016 对于解析日期,我使用:
4/5/2016 (d/M/yyyy) ' 4th May 2016
06/05/2016 (dd/MM/yyyy) ' 6th May 2016
05/8/2016 (dd/M/yyyy) ' 5th August 2016
6/08/2016 (d/MM/yyyy) ' 6th August 2016
对于解析日期,我使用:
Public Function GetDate(ByVal DateTxt As String) As Date
Dim date_ As Nullable(Of Date) = Nothing
Try
date_ = DateTime.ParseExact(DateTxt, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
Catch ex As Exception
End Try
If date_ Is Nothing Then
Try
date_ = DateTime.ParseExact(DateTxt, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
Catch ex As Exception
End Try
End If
If date_ Is Nothing Then
Try
date_ = DateTime.ParseExact(DateTxt, "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)
Catch ex As Exception
End Try
End If
If date_ Is Nothing Then
Try
date_ = DateTime.ParseExact(DateTxt, "d/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)
Catch ex As Exception
End Try
End If
Return date_
End Function
有没有更好的方法来解析这些类似类型的格式并获得准确的日期?无需在尝试使用
ParseExact
API将其与各种可能的模式进行匹配的地方放置这么多if-else块。我建议您直接使用TryParse
API,它将为您提供所需的帮助。请看下面的代码片段。这样,您还可以避免出现太多的try-catch块:
Public Function GetDate(ByVal DateTxt As String) As Date
Dim date_ As Nullable(Of System.DateTime) = Nothing
DateTime.TryParse(DateTxt, date_)
Return date_
End Function
使用
ParseExact
API,您无需将这么多if-else块放在试图将其与各种可能的模式匹配的位置。我建议您直接使用TryParse
API,它将为您提供所需的帮助。请看下面的代码片段。这样,您还可以避免出现太多的try-catch块:
Public Function GetDate(ByVal DateTxt As String) As Date
Dim date_ As Nullable(Of System.DateTime) = Nothing
DateTime.TryParse(DateTxt, date_)
Return date_
End Function
如果你不想把日期限制在特定的格式上,那么就用RBT的答案。但是,如果要将日期限制为指定格式,则应使用
DateTime
结构的TryParseExact
方法:
Public Function GetDate(ByVal DateTxt As String) As Nullable(Of Date)
Dim date_ As Date
Dim AcceptableFormats As String() = {"dd/MM/yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy"}
If Not DateTime.TryParseExact(DateTxt, AcceptableFormats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, date_) Then
Return Nothing
End If
Return date_
End Function
请注意,此函数返回类型为
可空(日期)
,而不是日期
如果不想将日期限制为特定格式,请使用RBT的答案。但是,如果要将日期限制为指定格式,则应使用DateTime
结构的TryParseExact
方法:
Public Function GetDate(ByVal DateTxt As String) As Nullable(Of Date)
Dim date_ As Date
Dim AcceptableFormats As String() = {"dd/MM/yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy"}
If Not DateTime.TryParseExact(DateTxt, AcceptableFormats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, date_) Then
Return Nothing
End If
Return date_
End Function
请注意,此函数返回类型是
null(of date)
,而不是date
可能与您在VB.NET中编写的代码重复,因此不要使用C标记,因为它不相关。包含日期作为文本字符串的数据库problem@Plutonix数据库非常旧(ms access 2000)。我正在将其导入SQL Server可能与您在VB.NET中编写的代码重复,因此不要使用C#标记,因为它不相关。以文本字符串形式包含日期的数据库problem@Plutonix数据库非常旧(ms access 2000)。我正在将其导入SQL server