LoginSignup
13
9

More than 5 years have passed since last update.

シェルスクリプトで日本時間のYYYYMMDDを返す

Last updated at Posted at 2019-03-27

実行環境に依存せずに、任意のタイムゾーンでの「今日の YYYYMMDD」を取得したい。

# 1. 今日の UNIX時刻(=UTCでの経過時間(秒)) を取得
unix_today=$(date +'%s')

# 2. タイムゾーンをズラす(+9:00 を秒に変換している)
unix_today=$((unix_today+32400))

# 3. YYYYMMDD に変換
jst_ymd_today=$(date '+%Y%m%d' --date "@$unix_today")

# 出力
echo $jst_ymd_today

出力

20190327

もっと短くかけないもんですかねえ。

追記

コメントで教えてもらいました。

# JST で今日の YYYYMMDD を得る
jst_ymd_today=$(TZ=UTC-9 date '+%Y%m%d')

# 出力
echo $jst_ymd_today
13
9
2

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
13
9