0

我正在寻找一种将军事时间字符串(“22:12:02”)转换为日期时间对象的方法。

下面是代码示例:

$TimePrevious = $Null
[array]$TimeArray = "22:12:02.000", "22:12:03.000", "23:12:02.000"

$TimeArray | ForEach-Object {
    $TimeCurrent = $_.Substring(0,8)
    $TimeDifference = [datetime]$TimeCurrent.TimeOfDay - [datetime]$TimePrevious.TimeOfDay
    If ($TimeDifference.Second -lt "10") {Write-Host "$_ is less than 10 seconds"}
    $TimePrevious = $_
    }

错误显示“无法将“System.TimeSpan”类型的“22:12:02”值转换为“System.DateTime”类型

4

2 回答 2

3

默认解析器似乎可以使用:

[datetime]"22:12:02"

2013 年 3 月 7 日星期四晚上 10:12:02

于 2013-03-07T23:16:54.173 回答
0

对不起。我刚刚开始工作:

$TimePrevious = $Null
[array]$TimeArray = "22:12:02.000", "22:12:03.000", "23:12:02.000"

$TimeArray | ForEach-Object {
    $TimeCurrent = $_.Substring(0,8)
    $TimeDifference = [datetime]$TimeCurrent - [datetime]$TimePrevious
    If ($TimeDifference.TotalSeconds -lt "10") {Write-Host "$_ is less than 10 seconds"}
    $TimePrevious = $_
    }

感谢您的快速回复。

于 2013-03-07T23:43:44.250 回答