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

linuxとかMacでN日前の日付を文字列で取得する方法メモ

Last updated at Posted at 2021-02-12

linuxでN日前の日付を文字列で取得する方法メモ
(Macの場合も以下の方法でしかできなかった。なぜ、、)

古いUNIX系の場合
一時的にタイムゾーンを設定して対応する。
※日本時間は世界標準時間 GMT (≒UTC) より9時間早いので、
 1日前 (24時間前)なら -9+24=15 をタイムゾーンに設定
 1日後 (24時間後)なら -9-24=-33 をタイムゾーンに設定

$ date +"%Y%m%d"
20090609
$ env TZ=JST-33 date +%Y%m%d
20090610
$ env TZ=JST-153 date +%Y%m%d
20090615
$ env TZ=JST+15 date +%Y%m%d
20090608
$ env TZ=JST+159 date +%Y%m%d
20090602

間に/をいれる事も可能

$ env TZ=JST-33 date +%Y/%m/%d
2021/02/13

参考

dateコマンドで○日前の日付を取得する
dateコマンドでバックアップファイル作成時にファイル名に日付_時間を付ける (LinuxとWindows用)

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?