如何解决错误“字符串未被识别为有效的DateTime”?

时间:2019-07-10 18:34:05

标签: powershell-4.0

我有三个从批处理文件作为字符串传递的日期类型变量。当他们进入PS脚本时,它们被视为:

  • 20190710 112538

  • 20190710 112538

  • 20190710

我面临的问题是,当我尝试解析它们时,收到一条错误消息,指出该字符串未被识别为有效的DateTime。

我尝试过将文化更改为不变的一种,并且没有区别。我确定我缺少什么。在成功解析这些字符串时,我可以获得一些帮助吗?

从此处发送的值:

set YYYY=%DATE:~-4%
set MM=%DATE:~4,2%
set DD=%DATE:~7,2%
set HH=%time:~0,2%
set NN=%time:~3,2%
set SS=%time:~6,2%
set MS=%time:~9,2%
SET "_oStart=%YYYY%%MM%%DD% %HH%%NN%%SS%%MS%" 

SET "_PSScript=C:\xx\xx.ps1"
SET "_oEnd=%YYYY%%MM%%DD% %HH%%NN%%SS%%MS%" 
SET "_oDateRan=%YYYY%%MM%%DD%"
SET "_PSCMD=Powershell -ExecutionPolicy Bypass -File "%_PSScript%" -oStart "%_oStart%" -oEnd "%_oEnd%" -oDateRan "%_oDateRan%"
Param(
    [String]$oStart,
    [String]$oEnd,
    [String]$oDateRan
)

[DateTime]$DateRan = [DateTime]::ParseExact($oDateRan, "yyyyMMdd", $null)
[DateTime]$StartTime = [DateTime]::ParseExact($oStart, "yyyyMMdd HHmmssff", $null)
[DateTime]$End = [DateTime]::ParseExact($oEnd, "yyyyMMdd HHmmssff", $null)

错误消息:

Exception calling "ParseExact" with "3" argument(s): "String was not
recognized as a valid DateTime."

0 个答案:

没有答案