calcute days between two dates:datediff.sh

example:./datediff.sh 20220917 20211110

#/bin/sh

THIS_PATH=$(cd `dirname $0`;pwd)  

cd $THIS_PATH  

##要求传入的数据格式为yyyyMMdd的两个开始和结束参数,如20160701 20160910  

start=$1  

end=$2  

##将输入的日期转为的时间戳格式  

startDate=`date -j -f “%Y%m%d” “${start}” +%s`  

endDate=`date -j -f “%Y%m%d” “${end}” +%s`  

##计算两个时间戳的差值除于每天86400s即为天数差  

stampDiff=`expr $endDate – $startDate`  

dayDiff=`expr $stampDiff / 86400`  

echo “间隔天数:$dayDiff”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.