将 DateFormat 与日期字符串进行比较

Comparing DateFormat with Date String

我有一个 Date = "20180719",我要从 XML 文件中取出它。我有一个下拉字段 'Manufacturing Date'(Windows 应用程序),其格式为“YYYYMMDD”。

现在我必须验证“日期”是否采用'Manufacturing Date'中提供的正确日期格式

有什么函数可以将 Date 转换为 DateFormat 吗?

谢谢!

XML 文件应该有一个用于验证的架构,以便可以捕获格式错误的日期(字符串!)。 如果您的 XML 可以包含惊喜,您必须检查 year/month/day-parts 的合理性。在代码中:

Dim aT : aT = Split("20180719 19072018 07192018 20181212 20181313 11112233 33331122")
Dim sD
For Each sD in aT
    WScript.Echo sD, verdict(sD)
Next

' "YYYYMMDD" format hard coded, using DateSerial to check the plausibility of the y-m-d-parts
Function verdict(sD)
  verdict = "invalid"
  If 8 = Len(sD) Then
     Dim nY : nY = CInt(Left(sD, 4))
     Dim nM : nM = CInt(Mid(sD, 5, 2))
     Dim nD : nD = CInt(Right(sD, 2))
     Dim dD : dD = DateSerial(nY, nM, nD)
     If sD = Year(dD) & Right(100 + Month(dD), 2) & Right( 100 + Day(dD), 2) Then verdict = "ok"
  End If
End Function

输出:

cscript 51613138.vbs
20180719 ok
19072018 invalid
07192018 invalid
20181212 ok
20181313 invalid
11112233 invalid
33331122 ok

此外,您可以对年份进行范围检查(使用数字 nY 变量)。但是有些边境案件(“20180102”的作者想指定一月还是二月的一天?)无法确定。