毎月の最初の日と最後の日

毎月の最初の日と最後の日

月と年という2つの数字が与えられた場合、その月の最初の日と最後の日をどのように計算しますか?私の目標は、次の3行を出力することです。

  1. Month/Year (月はテキスト形式ですが簡単です。)

  2. 毎月各日付:その日付は曜日の名前です:金曜日。 &土曜日。 &太陽。 [...]

  3. 月の日数:1&2&3 [...]&28&..?

dateGNUまたはBSD date(OS X)を使用するソリューションを探しています。

ベストアンサー1

# Last month:
l_first_date=$(date -d "`date +%Y%m01` -1 month" +%Y-%m-%d)
l_last_date=$(date -d "`date +%Y%m01` -1 day" +%Y-%m-%d)

# This month:
t_first_date=$(date +%Y-%m-01)
t_last_date=$(date -d "`date +%Y%m01` +1 month -1 day" +%Y-%m-%d)

# Next month:
n_first_date=$(date -d "`date +%Y%m01` +1 month" +%Y-%m-%d)
n_last_date=$(date -d "`date +%Y%m01` +2  month -1 day" +%Y-%m-%d)

# Print everything
echo "Last month: $l_first_date to $l_last_date"
echo "This month: $t_first_date to $t_last_date"
echo "Next month: $n_first_date to $n_last_date"

おすすめ記事