现在的位置: 首页 > 综合 > 正文

date命令计算日期跨天

2012年12月05日 ⁄ 综合 ⁄ 共 375字 ⁄ 字号 评论关闭

question:
需要知道某个日期前n天的日期

eg:
需要知道20110417前10天的日期是什么

方式1:
 date -d "-10 days 20110417" +%Y%m%d

方式2:

$now=20110417
#转为秒 
day_second=`date -d"$NOW" +%s`;
#计算20天的秒数 
seconds=`expr 86400 /* 10`
#计算20天前时间的秒 
history_second=`expr $day_second - $seconds`
#20天前应该显示的天 
#history_day=`date -d @$history_second +%Y%m%d` 与下面这个语句等同
history_day=`date -d"1970-01-01 UTC $history_second seconds" +%Y%m%d`
#搞定,输出20110407 
echo $ history_day

抱歉!评论已关闭.