使用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
# date +"%Y-%m-%d %H:%M:%s" -d "+10hour"
15
16
# date +"%Y-%m-%d %H:%M:%s" -d "+24hour"
19
37
# date +"%Y-%m-%d %H:%M:%s" -d "-24hour"
17
45
# date +"%Y-%m-%d %H:%M:%s" -d "-20minute"
04
58
00
# date +"%Y-%m-%d %H:%M:%s" -d "+20minute"
38
05
# date +"%Y-%m-%d %H:%M:%s" -d "+2day"
20
18
15
# date +"%Y-%m-%d %H:%M:%s" -d "-2day"
16
19
# date +"%Y-%m-%d %H:%M:%s" -d "-2month"
05
24
# date +"%Y-%m-%d %H:%M:%s" -d "+2month"
09
28
# date +"%Y-%m-%d %H:%M:%s" -d "+2year"
2014
32
# date +"%Y-%m-%d %H:%M:%s" -d "-2year"
2010
36
|
自己的练习
#!/bin/bashday=1echo $#if [[ "$#" -eq "1" ]];then{day=$1echo "day ago :" $day}fiday=$((day-1))dayT=`date +'%Y-%m-%d' -d "-$day days"`entime="$dayT 00:00"day=$((day+1))dayT=`date +'%Y-%m-%d' -d "-$day days"`bgtime="$dayT 00:00"date=$dayT