UNIX下獲取前一天後一天的日期

2021-09-09 06:25:54 字數 1469 閱讀 8714

在linux下-d選項

date  +"%y%m%d" -d  "+n days"         今天的後n天日期  

date +"%y%m%d" -d "-n days" 今天的前n天日期

在unix下沒有』-d』選項,所以無法使用上面的方法,

參照如下shell

#!/bin/sh

#輸入日期引數,不輸入預設當天

if [ $# -eq 1 ]

then

today=$1

else

today=`date +%y%m%d`

fi#獲取年月日

year=`echo $today|cut -c 1-4`

month=`echo $today|cut -c 5-6`

day=`echo $today|cut -c 7-8`

if [ $day -eq 1 ]

then

month=`expr $month - 1`

#月份減一後如果為0則為上一年,月份置12,年份減一

[ $month -eq 0 ] && month=12 && year=`expr $year - 1`

#獲取年月日曆

aaa=`cal $month $year`

#獲取最後一天

day=`echo $aaa|awk ''`

else

day=`expr $day - 1`

fi#如果日期只有一位則前補0

expr $day : "^.$" > /dev/null && day=0$day

expr $month : "^.$" > /dev/null && month=0$month

lastdate=$year$month$day

echo "lastdate="$lastdate

year=`echo $today|cut -c 1-4`

month=`echo $today|cut -c 5-6`

day=`echo $today|cut -c 7-8`

aaa=`cal $month $year`

bbb=`echo $aaa|awk ''`

[ $bbb -eq $day ] && month=`expr $month + 1` && day=0

[ $month -gt 12 ] && month=1 && year=`expr $year + 1`

day=`expr $day + 1`

expr $day : "^.$" > /dev/null && day=0$day

expr $month : "^.$" > /dev/null && month=0$month

nextdate=$year$month$day

echo "nextdate="$nextdate

exit 0

mysql獲取當前時間,前一天,後一天

負責的專案中,使用的是mysql資料庫,頁面上要顯示當天所註冊人數的數量,獲取當前的年月日,我使用的是 curdate 錯誤的sql語句 eg select count from user where registerdate curdate and registerdate curdate 1 雖...

mysql獲取當前時間,前一天,後一天

原文 負責的專案中,使用的是mysql資料庫,頁面上要顯示當天所註冊人數的數量,獲取當前的年月日,我使用的是 curdate 錯誤的sql語句 eg select count from user where registerdate curdate and registerdate curdate ...

mysql獲取當前時間,前一天,後一天

負責的專案中,使用的是mysql資料庫,頁面上要顯示當天所註冊人數的數量,獲取當前的年月日,我使用的是 curdate 錯誤的sql語句 eg select count from user where registerdate curdate and registerdate curdate 1 雖...