首页 > 解决方案 > 如果转换成功,我可以得到条件吗?

问题描述

这是示例:

Dim a As String = "2018-08-08"

Dim b As Date

我可以在 vb.net 中做到这一点吗?这是算法:

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

我希望你知道我的意思

标签: vb.net

解决方案


这就是类型的TryParseTryParseExact方法的DateTime用途。他们将尝试将 a 转换String为 aDateTime并返回Boolean表明它是否成功的 a。如果成功,DateTime则通过声明的参数输出ByRef,例如

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

推荐阅读