shell編寫時間四個季度,上週末等引數

mantoli發表於2020-10-23

#shell編寫時間四個季度,上週末等引數

1.設定四季度

轉化為標準語句為

#設定t-1天
last_day=`date -d  "-1 day" +'%Y%m%d'`
##shell編寫四個季度,方法1
#設定五個時間結點
one_time=`echo ${last_day:0:4}`"0101"		
two_time=`date -d "${last_day:0:4}" +'%Y0401'`
three_time=`echo ${last_day:0:4}`"0701"
four_time=`echo ${last_day:0:4}`"1001"
five_time=`date -d "${one_time} +12 month" +'%Y%m%d'`

if((${last_day} >= ${one_time})) && ((${last_day} < ${two_time}))
then
quarter=`echo ${last_day:0:4}`"-"Q1"
elif((${last_day} >= ${two_time})) && ((${last_day} < ${three_time}))
then
quarter=`echo ${last_day:0:4}`"-"Q2"
elif((${last_day} >= ${three_time})) && ((${last_day} < ${four_time}))
then
quarter=`echo ${last_day:0:4}`"-"Q3"
elif((${last_day} >= ${four_time})) && ((${last_day} < ${five_time}))
then
quarter=`echo ${last_day:0:4}`"-"Q4"
fi

##shell編寫四個季度,方法2
month=${last_day:4:2}
if[[${month} = "01" || ${month} = "02" || ${month} = "03" ]];then
quarter=`echo ${last_day:0:4}`"-"Q1"
fi

if[[${month} = "04" || ${month} = "05" || ${month} = "06" ]];then
quarter=`echo ${last_day:0:4}`"-"Q2"
fi

if[[${month} = "07" || ${month} = "08" || ${month} = "09" ]];then
quarter=`echo ${last_day:0:4}`"-"Q3"
fi

if[[${month} = "10" || ${month} = "11" || ${month} = "12" ]];then
quarter=`echo ${last_day:0:4}`"-"Q4"
fi

2.設定上週一,上週末的日期

#計算當前時星期幾
week=`date -d "last_day" +'%w'`
if [week = "0"];then
week=7
fi
#計算到距離上週一的差值
date_diff=`expr ${week} + "6"`
last_month=`date -d "${last_day} - ${date_diff} +'%Y%m%d'`
last_sunday=`date -d "${last_day} - ${week} +'%Y%m%d'`

3.設定上半月,下半月

date=${last_day:6:2}
if[[$(date -le "15")]];then
half_month=first_half_month
else
half_month=second_half_month
fi

相關文章