我正在寻找一种方法将军事时间字符串(“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“类型
答案 0 :(得分:3)
默认解析器似乎适用于:
[datetime]"22:12:02"
2013年3月7日星期四,下午10:12:02
答案 1 :(得分: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 = $_
}
感谢您的快速回复。