Get the App
SLTechnology News&Howtos  ›  Servers  › 

Get the date of the previous day and the day after under UNIX

Shulou Source: shulou.com Published: 2022-06-03 08:44:42 10月01日 Update

It is easy to solve this problem under Linux.

Date + "% Y%m%d"-d "+ n days" the last n days of today

Date + "% Y%m%d"-d "- n days" the first n days of today

There is no'- d 'option under UNIX, so the above method cannot be used

On the Internet, you can see that there is an adjustment through the time zone`TZ=aaa16 date +% Y%m%d\% echo% M:% S`

Note: date does not print the time of the local time zone, but the standard time, also known as London time.

London is in West Zone 1, while Beijing has eight time zones in East Zone 8.

TZ is a system environment variable that records the time zone in which the system is located. Aaa can be an arbitrary string, and 16 represents the time difference from the standard time.

Enter env | grep TZ output TZ=EAT-8

Similarly, the date of the following day can be obtained through echo `TZ=aaa-32 date +% Y%m%d\% Hava% M:% S`.

The above two are relatively simple acquisition methods. If you think this method is unreliable, you can write a shell script. You can refer to the following.

#! / enter a date parameter without entering the default current day if [$#-eq 1] then today=$1else today= `date +% Y% m% d`fi # to get 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` # if the month is minus one, it is the previous year, and the month is set to 12 Year minus one [$month-eq 0] & & month=12 & & year= `expr $year-1` # get the calendar aaa= `cal $month$ year` # get the last day day= `echo $aaa | awk'{print $NF} '`else day= `expr $day-1`fi # if there is only one date, the prefix 0expr $day: "^. $" > / dev/null & day=0$ dayexpr $month: "^. $" > / dev/null & month=0$ monthlastdate=$year$month$dayecho "lastdate=" $lastdateyear= `echo $today | cut-c 1-4`day = `echo $today | cut-c 5-6`day = `echo $today | cut-c 5-6`day = `echo $today C 7-8`aaa = `cal $month$ year`bbb = `echo $aaa | awk'{print $NF}'`[$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$ dayexpr $month: ^. $> / dev/null & month=0$ monthnextdate=$year$month$dayecho "nextdate=" $nextdateexit 0

Tags: Date time zone method input year month month standard standard time system London parameter variable only character string year year month day place calendar Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Microsoft Huawei MySQL