大家好,又见面了,我是你们的朋友全栈君。
需求
距离一个未来的日子还有多少天 距离一个过去的日子过去多少天
主要是用一个date命令
echo $(($(date --date="2022/01/01" +%s)/86400+1))
脚本写法:
#!/bin/bash
#name——timeDistance.sh
#function
#input a date, to calculate how many days to this day
#2022.1.18 daxiong
read -p "please input your date like YYYYMMDD ex>20150716 : " date2
#test whether or not have eight numbers
date_test=$(echo ${date2} | grep '[0-9] \ {8\} ')
if [ "${date_test}" == "" ] ; then
echo "you must input the right date format... "
exit 1
fi
#calculate
declare -i date_des=$(date --date="${date2}" +%s)
declare -i date_now=$(date +%s)
declare -i distanceSec=$((${date_des}-${date_now}))
declare -i distanceDay=$(( ${distanceSec}/60/60/24 ))
if [ "$distanceDay" -gt 0 ]; then
echo "That day hasn't come yet. "
echo "You have $((${distanceDay}+1)) days."
elif [ "$distanceDay" -eq 0 ]; then
echo "Just do it now!"
else [ "$distanceDay" -lt 0 ]
echo "That day has passed, $((0-${distanceDay})) days ago. "
fi
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168465.html原文链接:https://javaforall.cn