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: DATEコマンドで様々な日付を取得してみる

Posted at

バッチ作成で必要となって調べたついでにメモ。

実施環境:
Linux
[testuser@testhost ~]$ uname -a
Linux testhost 4.18.0-147.8.1.el8_1.x86_64 #1 SMP Thu Apr 9 13:49:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[testuser@testhost ~]$ echo $SHELL
/bin/bash
[testuser@testhost ~]$ date
Tue May 11 20:06:15 JST 2021

Linuxのdateコマンドは-dオプションに特定の文字列を指定することで、様々な日付を表示することが可能です。
ここではその一例を紹介します。

昨日 / 翌日:

"yesterday"や"tomorrow"を指定することで、昨日や翌日の日付を取得できます。

Linux
[testuser@testhost ~]$ date -d "yesterday"
Mon May 10 20:06:53 JST 2021
[testuser@testhost ~]$ date -d "tomorrow"
Wed May 12 20:06:53 JST 2021
N日前 / N日後:

"N day ago"だとN日前、"N day"だとN日後となります。
dayにsをつけても同じです。

Linux
[testuser@testhost ~]$ date -d "1 day ago"
Mon May 10 20:07:15 JST 2021
[testuser@testhost ~]$ date -d "1 days ago"
Mon May 10 20:07:10 JST 2021
[testuser@testhost ~]$ date -d "1 day"
Wed May 12 20:09:26 JST 2021
[testuser@testhost ~]$ date -d "1 days"
Wed May 12 20:09:31 JST 2021
N週 / N月 / N年:

"day"の代わりに"week"、"month"、"year"を使用すれば、それぞれ週、月、年単位での指定も可能です。

Linux
[testuser@testhost ~]$ date -d "1 week"
Tue May 18 20:32:36 JST 2021
[testuser@testhost ~]$ date -d "1 month"
Fri Jun 11 20:33:07 JST 2021
[testuser@testhost ~]$ date -d "1 year"
Wed May 11 20:33:20 JST 2021
N時 / N分 / N秒:

"day"の代わりに"hour"、"minute"、"second"を使用すれば、
それぞれ時、分、秒単位での指定も可能です

Linux
[testuser@testhost ~]$ date -d "1 hour"
Tue May 11 21:42:35 JST 2021
[testuser@testhost ~]$ date -d "1 minute"
Tue May 11 20:43:42 JST 2021
[testuser@testhost ~]$ date -d "1 second"
Tue May 11 20:42:58 JST 2021
曜日:

使い道はあまりないですが、曜日での指定もできます。
この場合、指定した曜日の日の中でも、当日以降で最も近い日が取得されます。
なお、この指定では時刻は00:00:00で取得されます。

Linux
[testuser@testhost ~]$ date -d "sunday"
Sun May 16 00:00:00 JST 2021
[testuser@testhost ~]$ date -d "monday"
Mon May 17 00:00:00 JST 2021
[testuser@testhost ~]$ date -d "tuesday"
Tue May 11 00:00:00 JST 2021
[testuser@testhost ~]$ date -d "wednesday"
Wed May 12 00:00:00 JST 2021
[testuser@testhost ~]$ date -d "thursday"
Thu May 13 00:00:00 JST 2021
[testuser@testhost ~]$ date -d "friday"
Fri May 14 00:00:00 JST 2021
[testuser@testhost ~]$ date -d "saturday"
Sat May 15 00:00:00 JST 2021
日付 / 時刻指定:

特定の日付や時刻を指定して取得することも可能です。
基本的な形式の日付なら、大体指定できます。
日付のみ指定した場合は時刻は00:00:00に、時刻のみ指定した場合は日付は当日のものになります。

Linux
[testuser@testhost ~]$ date -d "2020/1/1"
Wed Jan  1 00:00:00 JST 2020
[testuser@testhost ~]$ date -d "2020/01/01"
Wed Jan  1 00:00:00 JST 2020
[testuser@testhost ~]$ date -d "2020-1-1"
Wed Jan  1 00:00:00 JST 2020
[testuser@testhost ~]$ date -d "2020-01-01"
Wed Jan  1 00:00:00 JST 2020
[testuser@testhost ~]$ date -d "20200101"
Wed Jan  1 00:00:00 JST 2020
[testuser@testhost ~]$ date -d "200101"
Wed Jan  1 00:00:00 JST 2020
[testuser@testhost ~]$ date -d "09:00"
Tue May 11 09:00:00 JST 2021
[testuser@testhost ~]$ date -d "09:00:00"
Tue May 11 09:00:00 JST 2021
[testuser@testhost ~]$ date -d "0900"
Tue May 11 09:00:00 JST 2021
[testuser@testhost ~]$ date -d "2020/1/1 09:00:00"
Wed Jan  1 09:00:00 JST 2020
変数利用:

無論、変数も使用できます。

Linux
[testuser@testhost ~]$ NUM=7
[testuser@testhost ~]$ echo $NUM
7
[testuser@testhost ~]$ date -d "$NUM day ago"
Tue May  4 21:17:13 JST 2021
複数同時指定:

上記の指定のうち複数を同時に使用することも可能です。

Linux
[testuser@testhost ~]$ date -d "1 month ago 1 day"
Mon Apr 12 21:31:43 JST 2021
[testuser@testhost ~]$ date -d "2020/1/1 yesterday 1 month ago"
Sat Nov 30 00:00:00 JST 2019
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?