Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report