0
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.

Get-DateコマンドレットのFormatオプション"u"について

Posted at

PowerShellのGet-DateコマンドレットFormatオプションのu を利用した場合のメモ。

今回実行した環境

PowerShell 7.3.2

ドキュメント

Standard date and time format strings

タイムゾーンが大阪、札幌、東京の端末で実行してみる

Get-Date -Format "u"

現在のローカル時間をそのままオフセット考慮せずにutcとして出力するようです。

たとえば日本時間 2023年2月22日 09:00:00 にGet-Date -Format "u"と実行すると、2023-02-22 09:00:00Zと返却されます。

上記のケースで2023-02-22 00:00:00Zとしたい場合は、下記のいずれかの方法でできました。

  • Get-Date -AsUTC -Format "u" とAsUTCオプションを追加する。(AsUTCはPowerShell 7.1から追加されたオプション)
  • [DataTimeOffset]にしてから.ToString("u")で出力
Get-Date

Get-Date -Format "u"

# AsUTCはPowerShell 7.1で導入されたオプション
Get-Date -AsUTC -Format "u"

([DateTimeOffset](Get-Date)).ToString("u")

image.png

総評

PowerShellで日付周りは実際に実行してみると、自分だけかもしれませんが、そうなるのかといった動作がある気がします。
確認しながら、齟齬がないように気をつけたい所です。

0
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
0
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?