Linux shell 獲取給定日期的前(後)n 天(月)

場景用途:有時批量跑數據據就需要用到時間,一般要跑天的任務,有時還會跑下月度任務。這時就需要用到 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`





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章