LoginSignup
4
5

More than 5 years have passed since last update.

date コマンドで N ヶ月先の月末の日付を取得する方法

Last updated at Posted at 2013-09-20
date +'%Y-%m-%d' -d "1 days ago `date +%Y%m01 -d '+(N+1) month'`"

date +%Y%m01 -d '+(N+1) month' で (N+1)ヵ月先の初日が返ってきます。

1 days ago でその1日前の日付 = 月末の日付を参照することになります。

例 1: 来月の末日を取得したい場合

date +'%Y-%m-%d' -d "1 days ago `date +%Y%m01 -d '+2 month'`"

例 2: 今月の末日を取得したい場合

date +'%Y-%m-%d' -d "1 days ago `date +%Y%m01 -d '+1 month'`"

実行するとスクリーンショットのようになります。

date_command.png

動作確認は下記OSで行っています。
- Debian jessie
- CentOS 6

4
5
3

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
4
5