shell实现日期加减

do_date=`date -d '-1 day' +%F`
#获取前一天的月份
tomonth=`date -d '-1 day' +%Y-%m`

# 获取当前周几(周日为0)
whichday=`date -d $do_date +%w`;
echo "$whichday"
if [ $whichday == 0 ] ;then
whichday=7
fi
# 获取当前周一
#do_date_mo=`date -d "$do_date -$[${whichday}-1] days" +%F`
# 获取当前周日
#do_date_su=`date -d "$do_date +$[7-${whichday}] days" +%F`
#echo "当前周一日期:$do_date_mo"
#echo "当前周日日期:$do_date_su"

# 获取当前12自然周的日期范围
do_date_12week_start=`date -d "$do_date -$[${whichday}+76] days" +%F`
do_date_12week_end=`date -d "$do_date +$[7-${whichday}] days" +%F`
echo "获取当前12自然周的日期范围: $do_date_12week_start ~ $do_date_12week_end"

# 获取当前12自然月日期范围
do_date_m=`date -d "$do_date" +%Y-%m-01`
do_date_12month_start=`date -d "$do_date_m -11 month" +%F`
next_month_begin=`date -d "$do_date_m +1 month" +%F`
do_date_12month_end=`date -d "$next_month_begin -1 days" +%F`
echo "获取当前12自然月的日期范围: $do_date_12month_start ~ $do_date_12month_end"

你可能感兴趣的:(数仓,大数据,shell,日期函数)