弄清楚Linux中剩余的时间

blo*_*m17 1 unix linux shell

好吧,我所处的情况是,我想弄清楚在2013年5月15日这个特定日期之前剩下多少天.日期并不重要,但想法是弄清楚剩下的天数在到达那天之前.

我想做这样的事情(伪代码):

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
Run Code Online (Sandbox Code Playgroud)

现在我不知道是否有更简单的方法可以做到这一点,因为我对Linux和shell脚本非常陌生,所以如果有更好的方法可以帮助我.

Kar*_*ath 5

这个根据unix时间戳的差异来计算它:

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

1如果分数日计为一整天,则必须添加分区.