Powershellでよく使う、時刻関連の小技をメモしておきます
現在時刻の日付文字列取得
Function Get-DateString { # return DateTime String # EX) 2015/04/01 12:00:00 [String] ( ([DateTime]::Now).ToString(`yyyy/MM/dd HH:mm:ss`) ) } Get-DateString
Unixエポック(秒)に変換
Function ConvertTo-UnixEpoch { Param( $DateTime ) # Unix Epoch Origin [DateTime] $EpochOrigin = `1970/01/01 00:00:00` # return unix epoch time (New-TimeSpan -Start (Get-Date $EpochOrigin) -End (Get-Date ([DateTime] $DateTime))).Totalseconds } ConvertTo-UnixEpoch `2015/04/01 12:00`
ISO8601形式に変換
Function ConvertTo-ISO8601 { Param( $DateTime ) # TimeZone [String] $tz = '+09:00' # return ISO8601 format # EX) 2015-09-20T22:19:43+09:00 ([DateTime] $DateTime).ToString(`yyyy-MM-ddTHH:mm:ss$tz`) } ConvertTo-ISO8601 `2015/04/01 12:00`
もしくはこちら
Function ConvertTo-ISO8601 { Param( $DateTime ) # return ISO8601 format # EX) 2015-04-01T12:00:00.0000000 ([DateTime] $DateTime).ToString(`o`) } ConvertTo-ISO8601 `2015/04/01 12:00`
検証環境
Windows7 Powershell 4.0 (PS2.0でも動くと思います)