歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 用date獲得前一天的日期

用date獲得前一天的日期

日期:2017/2/27 14:15:33   编辑:更多Linux
  好象有date 有-24 有+%y%m%d等,謝了。 還有有辦法獲得3天前的日期嗎? 唉,在b shell沒有這個用法了,要想用純shell做幾天前後的運算,只有自己想辦法了,+%y%m%d用於控制輸出格式,作用有限,要想做的話,參見這個帖子大家的發言: 我的確在哪兒看到用一條命令就可以取昨天或明天的日期, 參數中有-24或+24。沒那麼復雜 好象用到了參數TZ=-24;eXPort TZ什麼的 改變時區,應該可以實現,確實是個好方法。 不會吧,你說的俺都明白了,你自己還不懂? date命令的顯示是與環境變量TZ有關的 看以下的命令操作,你應該便明白了, $#看當前時區 $echo $TZ CST-8 $#顯示當前時間 $date Mon Apr 2 15:48:36 CST 2002 $#改變當前時區, TZ=CST+16;export TZ $#顯示當前時間(中間未改變系統時間,但date命令的顯示已為昨天) Mon Apr 1 15:48:33 CST 2002 #!/bin/sh # ydate: A Bourne shell script that # prints yestarday's date # Output form: Month Day Year # From Focus on Unix: http://unix.about.com # Set the current month day and year. month=`date +%m` day=`date +%d` year=`date +%Y` # Add 0 to month. This is a # trick to make month an unpadded integer. month=`expr $month + 0` # SuBTract one from the current day. day=`expr $day - 1` # If the day is 0 then determine the last # day of the previous month. if [ $day -eq 0 ]; then # Find the preivous month. month=`expr $month - 1` # If the month is 0 then it is Dec 31 of # the previous year. if [ $month -eq 0 ]; then month=12 day=31 year=`expr $year - 1` # If the month is not zero we need to find # the last day of the month. else case $month in 135781012) day=31;; 46911) day=30;; 2) if [ `expr $year % 4` -eq 0 ]; then if [ `expr $year % 400` -eq 0 ]; then day=29 elif [ `expr $year % 100` -eq 0 ]; then day=28 else day=29 fi else day=28 fi ;; esac fi fi # Print the month day and year. echo $month $day $year exit 0 時區轉換實在是太精妙了!:-) 如果在shell中改了時區,執行完後,系統真正的時區還是正確的嗎? 回樓上:最好你要把TZ的設置該回去了。 不過我在實踐中用的方法是: 如果你的主機能夠連接到數據庫的話,比如可以連接到SYBASE數據庫,那就可以利用數據庫裡面計算時間的豐富的函數了,比如 dateadd(day,getdate(),-1) 就能得到最天的日期了 因為這個貼子被版主蓋過章,所以在此跟一下。 最近經常看到有網友關於相對日期問題的貼子,這在shell下進行解決時比較頭疼。現將我作的幾個程序貼出來與大家分享。雖然是用c寫的,把它們貼在這裡,我想大家也不會太反對的。其中一個程序以前發表在sco版下,現在早已淹沒在汪洋大海中了,無從查找。 因為是早先剛學c時的作品,且英文又不行,故寫出的程序比較難看,請大家別見笑。 相關鏈接: date=`expr `date +%m%d` - 1`


這兒有一個我寫的計算昨天的函數,可能是純shell中唯一的簡單解決方案吧,要想將其應用在計算任意天前後,恐怕還是不太方便的,看看,能否滿足? yesterday () { _year=$1;_month=$2;_day=$3 if [ $_day -eq 1 ];then if [ $_month -eq 1 ];then _year=`expr $_year - 1` _month=12 else _month=`expr $_month - 1` fi _day=`echo `cal $_month $_year`awk '{print $NF}'` else _day=`expr $_day - 1` fi echo "$_year $_month $_day"awk '{if (length($2)==1) $2=0$2;if (length($3)==1) $3=0$3;print $1,$2,$3}' } unix 我沒有試過,Linux上可以用 date --date "1 days ago" yy=`date +%Y` #Year yyyy mm=`date +%m` #Month mm dd=`date +%d` #Day dd if [ $dd = "01" ] #如果為月初則計算上月末的日期 then lm=`expr $mm - 1 ` #lm 上月 if [ $lm -eq 0 ] then lm=12 fi case $Fm in 135781012) Yesterday=31 ;; 46911) Yesterday=30 ;; 2) #計算閏月 if [ ` expr $yy % 4 ` -eq 0 -a `expr $yy % 100 ` -ne 0 -o ` expr $yy % 400 ` -eq 0 ] then Yesterday=29 else Yesterday=28 fi ;; esac else #如果不是月初的處理 Yesterday=`expr $dd - 1 ` if [ $Yesterday -lt 10 ] ###上旬日期處理 then Yesterday=0$Yesterday fi fi 結果: today=20020301 yesterday=28



case $Fm in 135781012) Yesterday=31 ;; 46911) Yesterday=30 ;; 2) #計算閏月 if [ ` expr $yy % 4 ` -eq 0 -a `expr $yy % 100 ` -ne 0 -o ` expr $yy % 400 ` -eq 0 ] then Yesterday=29 else Yesterday=28 fi ;; esac else #如果不是月初的處理 Yesterday=`expr $dd - 1 ` if [ $Yesterday -lt 10 ] ###上旬日期處理 then Yesterday=0$Yesterday fi fi 結果: today=20020301 yesterday=28



Copyright © Linux教程網 All Rights Reserved