3
3

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 5 years have passed since last update.

日付を出力するときのフォーマットをカスタムする

Last updated at Posted at 2019-10-09

はじめに

  • コマンドプロンプト、PowerShell両方で実行可能。
  • 今回はコマンドプロンプトで実行している。

    ※「powershell」の記述を抜けばPowerShellで実行可能。

日付のフォーマットを変更する。

「yyyymmdd」形式や昨日の日付などを出力できる。

本日の日付を「yyyymmdd」形式で出力する

powershell [DateTime]::Today.ToString('yyyyMMdd')

昨日の日付を「yyyy-mm-dd」形式で出力する

powershell [DateTime]::Today.AddDays(-1).ToString('yyyy-MM-dd')

実行結果を変数として代入する

batファイルなどに使うと便利。
「YDAY」に昨日の日付を「yymmdd」形式で出力する。

set CMD="powershell [DateTime]::Today.AddDays(-1).ToString('yyMMdd')"
set YDAY=
for /f "usebackq delims=" %%a in (`%CMD%`) do set YDAY=%%a
echo %YDAY%

pause

endlocal
3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?