Vb.net 如果转换成功,我可以获得条件吗?

Vb.net 如果转换成功,我可以获得条件吗?,vb.net,Vb.net,例如: Dim a As String=“2018-08-08” Dim b As Date 我可以在vb.net中执行此操作吗?这是算法: If (b=a) is success Then ......... End If 我希望你知道我的意思这就是TryParse和TryParseExact日期时间类型的方法的用途。他们将尝试将字符串转换为日期时间,并返回一个布尔值,指示是否成功。如果成功,则通过一个由ref声明的参数输出日期时间,例如: If Date.TryParse(a, b) Th

例如:

Dim a As String=“2018-08-08”

Dim b As Date

我可以在vb.net中执行此操作吗?这是算法:

If (b=a) is success Then
.........
End If

我希望你知道我的意思

这就是
TryParse
TryParseExact
日期时间类型的方法的用途。他们将尝试将
字符串
转换为
日期时间
,并返回一个
布尔值
,指示是否成功。如果成功,则通过一个由ref声明的参数输出
日期时间
,例如:

If Date.TryParse(a, b) Then
    'The conversion was successful and 'a' contains the Date value.
Else
    'The conversion failed and 'a' contains the default value for a Date.
End If

正如@đěxěŕ所建议的那样,如果您希望使用一种或多种特定格式,而不希望接受任何其他格式,我建议您通常应该使用
TryParseExact
TryParse
适用于各种格式,但取决于本地机器的特定区域设置。