date

プロセスとシステム

書き方

date [+FORMAT]

いまの時刻を好きな形式で表示し、日付の計算もします。`+` のあとの書式はどこでも同じですが、日付をずらすオプションは GNU が `-d`、macOS/BSD が `-v` と `-j -f` でまったく違います。

よく使うオプション

オプション意味
+%FShorthand for %Y-%m-%d.
+%sUnix epoch seconds right now.
-uWork in UTC instead of local time.
-d "yesterday"Parse or shift a date. GNU only.
-d @1700000000Turn epoch seconds into a readable date. GNU only.
-v-2dShift by two days on macOS/BSD, which has no -d.
-r 1700000000Read epoch seconds on macOS/BSD.
-j -f "%Y-%m-%d"Parse a string without setting the clock, on macOS/BSD.

date -u +"%Y-%m-%dT%H:%M:%SZ"

An ISO 8601 timestamp in UTC, the same on both platforms.

date -d "2 days ago" +%F

The date two days back on GNU; on macOS it is date -v-2d +%F.

date +%s

Epoch seconds, for a filename or a quick difference.

何が動いていて何が資源を食っているかを見ます。止める前に何なのかを確かめるのが順番です。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. date は何をしますか。

いまの時刻を好きな形式で表示し、日付の計算もします。`+` のあとの書式はどこでも同じですが、日付をずらすオプションは GNU が `-d`、macOS/BSD が `-v` と `-j -f` でまったく違います。

Q. どう打ちますか。

date [+FORMAT] — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは8個です。全部は man date にあります。このコマンドはプロセスとシステムの仲間です。

関連するコマンド

man ページ: man date