date+%s可以得到UNIX的时间戳;
用shell将时间字符串与时间戳互转:
date-d"2010-10-1800:00:00"+%s输出形如:1287331200
而时间戳转换为字符串可以这样做:
date-d@1287331200"+%Y-%m-%d"输出形如:2010-10-18
如果需要得到指定日期的前后几天,可以:
1、seconds=`date-d"2010-10-1800:00:00"+%s`#得到时间戳
2、seconds_new=`expr$seconds+86400`#加上一天的秒数86400
3、date_new=`date-d@$seconds_new"+%Y-%m-%d"`#获得指定日前加上一天的日前
第一种 使用date -d 选项
date +"%Y%m%d" -d "+n days" 今天的后n天日期
date +"%Y%m%d" -d "-n days" 今天的前n天日期
- 自己写函数
- #!/bin/ksh
- #获取昨天的日期
- lastday()
- {
- Today=`date+%Y%m%d`
- YEAR=`echo$Today|cut-c1-4`
- MONTH=`echo$Today|cut-c5-6`
- DAY=`echo$Today|cut-c7-8`
- if[$DAY-eq1]
- then
- if[$MONTH-eq1]
- then
- YEAR=`expr$YEAR-1`
- MONTH=12
- else
- MONTH=`expr$MONTH-1`
- fi
- #如下两种方法皆可
- #DAY=`echo\`cal$MONTH$YEAR\`|tail-n1|awk'{print$NF}'`
- #
- DAY=$(cal$MONTH$YEAR|tail-n2|sed-n1,1p|awk'{print$NF}')
- else
- DAY=`expr$DAY-1`
- fi
- LAST=$(echo"$YEAR$MONTH$DAY"|awk'{if(length($2)==1)$2=0$2;if(length($3)==1)$3=0$3;printf"%s%s%s",$1,$2,$3}')
- }
- <prename="code"class="python">GetLastDate()
- {
- TODAY=`date"+%Y%m%d"`
- LASTDATE=`echo$TODAY|awk\
- '{\
- Year=$1;Month=$2;Day=$3;\
- M[1]=31;M[2]=28;M[3]=31;M[4]=30;M[5]=31;M[6]=30;\
- M[7]=31;M[8]=31;M[9]=30;M[10]=31;M[11]=30;M[12]=31;\
- if(Year%4==0&&(Year%100!=0||Year%400==0))M[2]=29;\
- elseM[2]=28;\
- if(Day-1==0)\
- {\
- if(Month-1==0){Year-=1;Month=12;}\
- elseMonth-=1;\
- Day=M[Month];\
- }\
- elseDay-=1;\
- printf("%04d%02d%02d",Year,Month,Day);\
- }'`
- }
- echo"***************"
- lastday
- echo${LAST}
- echo"***************"
- GetLastDate
- echo${LASTDATE}</pre><prename="code"class="python">-----------------------------------------------------------</pre><prename="code"class="python">常用日期格式</pre><prename="code"class="python"></pre><prename="code"class="php">[root@localhosttmp]#date+"%F"
- 2012-06-08
- [root@localhosttmp]#date+"%D"
- 06/08/12
- [root@localhosttmp]#date+"%Y%m%d%H:%M:%S"
- 2012060804:52:28
- [root@localhosttmp]#date+"%Y%m%d"
- 20120608
- [root@localhosttmp]#date+"%Y%m%d%H:%M:%S"-d"+1hours"
- 2012060805:52:43
- [root@localhosttmp]#date+"%Y%m%d%H:%M:%S"-d"+1days"
- 2012060904:52:52
- [root@localhosttmp]#date+"%Y%m%d%H:%M:%S"-d"+1months"
- 2012070804:52:56
- [root@localhosttmp]#date+"%Y%m%d%H:%M:%S"-d"+1years"
- 2013060804:53:07</pre><prename="code"class="php">[root@localhosttmp]#date+"%T"
- 05:03:21
- [root@localhosttmp]#date+"%F%T"
- 2012-06-0805:03:34
- [root@localhosttmp]#date+"%D%T"
- 06/08/1205:03:39</pre><prename="code"class="php"><pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"
- 2012-07-1805:17:01
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"+10hour"
- 2012-07-1815:17:16
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"+24hour"
- 2012-07-1905:17:37
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"-24hour"
- 2012-07-1705:17:45
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"-20minute"
- 2012-07-1804:58:00
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"+20minute"
- 2012-07-1805:38:05
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"+2day"
- 2012-07-2005:18:15
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"-2day"
- 2012-07-1605:18:19
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"-2month"
- 2012-05-1805:18:24
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"+2month"
- 2012-09-1805:18:28
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"+2year"
- 2014-07-1805:18:32
- [root@localhostriqi]#date+"%Y-%m-%d%H:%M:%S"-d"-2year"
- 2010-07-1805:18:36
---------------------------------------------------