#示例使用的时区;没有它时使用本地时区
export TZ=UTC
#固定日期使输出保持一致;要用今天的日期,则去掉日期,
#只留下单词:date -d "yesterday"、date -d "+1 month"
now="2024-02-28"
#GNU date -d 能理解用单词写的日、月和年
yesterday=$(date -d "$now -1 day" +%F)
#yesterday is 2024-02-27
tomorrow=$(date -d "$now +1 day" +%F)
#明天是 2024-02-29,闰年
nextMonth=$(date -d "$now +1 month" +%F)
#nextMonth is 2024-03-28
nextYear=$(date -d "$now +1 year" +%F)
#nextYear is 2025-02-28
#陷阱:+1 month 保持日期不变,2 月 31 日会顺延到 3 月
fromJanuary31=$(date -d "2024-01-31 +1 month" +%F)
#fromJanuary31 is 2024-03-02
#bash 的做法:自 1970 年起的秒数加上换算成秒的天数
start=$(date -d "$now 12:00" +%s)
printf -v in100Days '%(%F)T' "$((start + 100 * 24 * 60 * 60))"
#in100Days is 2024-06-07
echo "now is $now"
echo "yesterday is $yesterday"
echo "tomorrow is $tomorrow"
echo "nextMonth is $nextMonth"
echo "nextYear is $nextYear"
echo "fromJanuary31 is $fromJanuary31"
echo "in100Days is $in100Days"