場景用途:有時批量跑數據據就需要用到時間,一般要跑天的任務,有時還會跑下月度任務。這時就需要用到 shell 對時間的操作。
獲取給定日期的前 n 天的日期:
currentDay="20170801"
n=3
nDaysAgo=`date -d "${currentDay} $n days ago" +%Y%m%d`
獲取給定日期的後 n 天的日期:
currentDay="20170801"
n=3
nDaysAfter=`date -d "${currentDay} $n days" +%Y%m%d`
獲取給定日期的前 n 月的日期:
currentDay="20170801"
n=-3
nMonthsAgo=`date -d "${currentDay} $n months " +%Y%m%d`
獲取給定日期的後 n 月的日期:
currentDay="20170801"
n=3
nMonthsAfter=`date -d "${currentDay} $n months " +%Y%m%d`