计算月份差
monthdiff.sh
function getMonthDiff() {
date1=${1//-/}
date1_year=${date1:0:4}
date1_month=${date1:4:2}
date2=${2//-/}
date2_year=${date2:0:4}
date2_month=${date2:4:2}
diff=$(((date1_year-date2_year)*12 + (date1_month-date2_month)))
echo $diff
}
date1=$1
date2=$2
echo $(getMonthDiff $date1 $date2)
计算天数差
dayDiff.sh
function getDayDiff(){
day1=$1
day2=$2
time1=`date +%s -d "$day1"`
time2=`date +%s -d "$day2"`
days=$((($time1-$time2)/86400))
echo $days
}echo $(getDayDiff $1 $2)