UNIX脚本,用于计算一个人的生日之前还剩下多少天和几小时

时间:2019-02-17 13:41:18

标签: bash unix unix-timestamp

我正在尝试制作一个UNIX脚本,该脚本计算出用户输入生日之前还剩下多少天和几小时。我只能让用户输入他的生日,并将其分为月,日和当前日期信息,但我认为我的减法有误

echo -n "Enter the birthdate (mm-dd-yyyy): "
read bdate
bmonth=${bdate:0:2}
bday=${bdate:3:2}
byear=${bdate:6:4}`
`cdate='date +%m-%d-%Y'/
cmonth=${cdate:0:2}
cday=${cdate:3:2}
cyear=${cdate:6:4}
diffdays=$(( ($bdate - $cdate) / (60*60*24) ))
echo $difdays

1 个答案:

答案 0 :(得分:0)

下面的工作。

#!/bin/bash
## Just using a YYYY-MM-DD format
echo -n "Enter the birthdate (YYYY-MM-DD):"
read bdate
## convert to epoch time
bdate=`date -d "${bdate}" +"%s"`
## convert to epoch time
current_date=`date  +"%s"`
## perform a calculation divide to find the no. of days
echo $(($((bdate - current_date)) / 86400))