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

The default parser seems to work with that:

[datetime]"22:12:02"

Thursday, March 07, 2013 10:12:02 PM

于 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 に答える