2012-07-06 127 views
1

好的,我现在的情况是我想知道在2013年5月15日之前的特定日期,究竟有多少天。剩下的日期并不重要,计算出达到那一天之前的剩余天数。找出剩余时间在Linux中的时间

我觉得做这样的(伪代码):

y=$end_year-$cur_year 

if [ y -ge 1 ]; then 
    days=$y*365 
else 
    continue 
fi 

if [ $end_month -gt $cur_month ]; then 
    m=$end_month-$cur_month 
else 
    contine 
fi 

if [ $end_day -gt $cur_day ]; then 
    d=$end_day-$cur_day 
else 
    continue 
fi 

result=$days+$m+$d 

现在我不知道是否有这样做的更简单的方法,因为我很新的Linux和shell脚本,所以如果有一个更好的方法来做到这一点,请帮助我。

回答

5

这一个从UNIX时间戳的差来计算它:

date 
Fri Jul 6 15:04:04 BST 2012 
echo $(((`date -d "May 15, 2013" +'%s'` - `date +'%s'`)/(60*60*24))) 
312 

司地板,所以你必须添加1,如果分数天计为一整天。

1
[06 Jul 2012 18:13:47] [email protected] ~ 
$ DIFF=$(($(date -d '05/15/2013 00:00' +%s) - $(date +%s))) ; \ 
    echo $((DIFF/(3600*24))) days $((DIFF % (3600*24)/3600)) \ 
    hours $((DIFF % 3600/60)) minutes $((DIFF % 60)) seconds left 
312 days 5 hours 46 minutes 13 seconds left