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?

Powershellで1日前の日付文字列取得

Posted at

ファイルのコピーをする際に格納フォルダに年月とかつけてほしいという依頼があり、実行日の前日の月などで作ってほしい、という依頼がありました。
コピーだけなのでBATで作ろうとしたのですが、1日前の月などを計算するのがBATでは相当大変そうだったのですが、PowerShellでは結構簡単にできました。

1日前の年

$last_yyyy_str = (Get-Date -Day 1).AddDays(-1).ToString("yyyy")

1日前の年月

$last_yyyymm_str = (Get-Date -Day 1).AddDays(-1).ToString("yyyyMM")

1日前の年月日

$last_yyyymmdd_str = (Get-Date -Day 1).AddDays(-1).ToString("yyyyMMdd")

古めのPowerShellでも動くと思います。
こちらを参考にしたと思います:
https://stackoverflow.com/questions/61189531/powershell-get-date-adddays-how-to-get-desired-format

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?