使用date -d 选项:
date +"%Y%m%d" -d "+n days" 今天的后n天日期
date +"%Y%m%d" -d "-n days" 今天的前n天日期
比如:
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S"
2012-07-18 05:17:01
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "+10hour"
2012-07-18 15:17:16
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "+24hour"
2012-07-19 05:17:37
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "-24hour"
2012-07-17 05:17:45
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "-20minute"
2012-07-18 04:58:00
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "+20minute"
2012-07-18 05:38:05
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "+2day"
2012-07-20 05:18:15
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "-2day"
2012-07-16 05:18:19
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "-2month"
2012-05-18 05:18:24
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "+2month"
2012-09-18 05:18:28
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "+2year"
2014-07-18 05:18:32
[root@localhost riqi]# date +"%Y-%m-%d %H:%M:%S" -d "-2year"
2010-07-18 05:18:36