Today's date, minus X days in shell script

后端 未结 5 2001
小鲜肉
小鲜肉 2020-12-08 09:25

I need to create three variables, each for Year, Month, and Day for Today\'s date, minus X number of days. For this question I\'ll choose a random amount of days: 222.

5条回答
  •  囚心锁ツ
    2020-12-08 10:08

    epoch=$(( `date '+%s'` - ( 24 * 60 * 60 * 222 ) ))
    year=`date -d "@$epoch" '+%Y'`
    month=`date -d "@$epoch" '+%m'`
    day=`date -d "@$epoch" '+%d'`
    

    Should do the trick.

提交回复
热议问题