将军事时间字符串转换为日期时间



我正在寻找将军事时间字符串("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 "的"22:12:02"值。时间跨度"到类型"系统。DateTime "

默认解析器似乎可以处理:

[datetime]"22:12:02"

星期四,March 07, 2013 10:12:02 PM

对不起。我刚刚让这个工作:

$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 = $_
    }

感谢您的快速回复。

相关内容

  • 没有找到相关文章

最新更新