1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PowerShellでローカルタイムを計算してみる

Posted at

PowerShellでGet-Dateコマンドレットを実行すると現在端末に設定されているタイムゾーンの時刻を返してくれますが。

今回はこれを各国のローカルタイムに変換してみます。

System.TimeZoneInfoのConvertTimeBySystemTimeZoneIdを使う

TimeZoneInfo.ConvertTimeBySystemTimeZoneId メソッド

.NETではConvertTimeBySystemTimeZoneIdを利用して下記のように、変換したい日付とタイムゾーンIDを渡すると変換できる。(UTCとEST)

# UTCに変換
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId( (Get-Date), 'Greenwich Standard Time')

# ESTに変換
    [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId( (Get-Date), 'Eastern Standard Time')

image.png

タイムゾーンIDの一覧を確認してみる

変換に利用するタイムゾーンIDの一覧を確認するには下記コマンドから確認できる。

# GetSystemTimeZonesメソッドを実行
[System.TimeZoneInfo]::GetSystemTimeZones()

# Get-TimeZoneコマンドレットのListAvailableオプションを利用
Get-TimeZone -ListAvailable

総評

PowerShellの力というか.NETの力といった感じですが。
PowerShellは簡単に.NETを利用できるのはやはり便利だと感じます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?