计算月份差
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)
计算季度
比如给出月份得到当前季度的最后一个月 202301->202303
PERIOD_ID=$1
QUARTER_LAST_MONTH=$(((${PERIOD_ID:4:2}-1)/3*3+3))
if [ ${#QUARTER_LAST_MONTH} -eq 1 ]; then
QUARTER_LAST_MONTH='0'${QUARTER_LAST_MONTH}
fi
V_QUARTER_LAST_PERIOD_ID=${PERIOD_ID:0:4}${QUARTER_LAST_MONTH}
echo ${V_QUARTER_LAST_PERIOD_ID}
当我试到07的时候差点就觉得没问题了。结果还是除了问题 08为啥不行呢?-bash: 08: value too great for base (error token is “08“)_"value too great for base (error token is \"08\")"_lijun_work的博客-CSDN博客