Get Yesterday Date on Solaris

I am running SunOS.

bash-3.00$ uname -a SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc 

I need to find Yesterday date in linux with the correct formatting passed from the command line. When I tried this on the command line -

 bash-3.00$ date --date='yesterday' '+%Y%m%d' date: illegal option -- date=yesterday usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff] 

I always get the date of the illegal option , why is this so? Something is wrong, what am I doing?

Update: -

 bash-3.00$ date --version date: illegal option -- version usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff] 
+6
source share
7 answers

Try it below. He should work

 YESTERDAY=`TZ=GMT+24 date +%Y%m%d`; echo $YESTERDAY 
+8
source

Try it:

 DATE_STAMP=`TZ=GMT+24 date +%Y%m%d` 

where GMT is the time zone, and you may need to change 24 according to the time difference you received from GMT. Either this, or you can change GMT in a time zone that is more convenient for you, for example. Cst

+3
source

As suggested by larsks, you can use perl:

 perl -e 'use POSIX qw(strftime); print strftime "%a %b %e %H:%M:%S %Y",localtime(time()- 3600*24);' 

Slightly modified from

http://blog.rootshell.be/2006/05/04/solaris-yesterday-date/

To get the format YYYYMMDD, use this

 perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d",localtime(time()- 3600*24);' 

This link explains how to format date and time using strftime

http://perltraining.com.au/tips/2009-02-26.html

+2
source

Clean bash solution

 #!/bin/bash # get and split date today=`date +%Y%m%d` year=${today:0:4} month=${today:4:2} day=${today:6:2} # avoid octal mismatch if (( ${day:0:1} == 0 )); then day=${day:1:1}; fi if (( ${month:0:1} == 0 )); then month=${month:1:1}; fi # calc day=$((day-1)) if ((day==0)); then month=$((month-1)) if ((month==0)); then year=$((year-1)) month=12 fi last_day_of_month=$((((62648012>>month*2&3)+28)+(month==2 && y%4==0))) day=$last_day_of_month fi # format result if ((day<10)); then day="0"$day; fi if ((month<10)); then month="0"$month; fi yesterday="$year$month$day" echo $yesterday 
+2
source

TZ=$TZ+24 date +'%Y/%m/%d' on SunOS.

+1
source

When playing Solaris10 with a non-GMT environment, I get the following:

 # date Fri Jul 26 13:09:38 CEST 2013 (OK) # (TZ=CEST+24 date) Thu Jul 25 11:09:38 CEST 2013 (ERR) # (TZ=GMT+24 date) Thu Jul 25 11:09:38 GMT 2013 (OK) # (TZ=CEST+$((24-$((`date "+%H"`-`date -u "+%H"`)))) date) Thu Jul 25 13:09:38 CEST 2013 (OK) 

As you can see, I have, and I want to get CEST, but TZ = CEST + 24 gives me the wrong CEST data; GMT + 24 gives me the correct data, but not applicable.

To get the correct result, I have to use GMT + 22 (the wrong command, the correct result) or CEST + 22 (the wrong value, but the final result is correct for the correct TZ)

0
source

The pure bash solution given by @olivecoder is very reliable compared to any other solution, but there is a bug that needs to be fixed. when the day of the 1st day comes, the script fails by specifying " last_day_of_month " in the daily value. @olivecoder missed $ in day=last_day_of_month what it should be

 day=$last_day_of_month; 

After this correction, it works very well.

Using Timezone -24 has some problem based on time when using it. in some cases, he goes the day before yesterday. Therefore, I think that it is not reliable.

0
source

Source: https://habr.com/ru/post/922322/


All Articles