6

I am running SunOS.

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

I need to find Yesterday's date in linux with the proper formatting passed from command prompt. When I tried like this on my shell prompt-

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 date illegal option, why is it so? Is there anything wrong I am 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
  • That works fine for me under linux Commented Aug 7, 2012 at 22:35
  • That is not working for me. Is there any information you need from me, like why it is not working? Commented Aug 7, 2012 at 22:37
  • What does date --version say? (post the results in your question, not in a comment because formatting will get hosed) Commented Aug 7, 2012 at 22:38
  • @Jon, I updated the question with your command output. Commented Aug 7, 2012 at 22:41
  • 3
    You're not using GNU date, so you're not going to have access to all of the fancy options people are talking about. You can install GNU date for Solaris, or you could write a small Perl/Python/etc script that could do the same thing. Commented Aug 7, 2012 at 22:53

7 Answers 7

10

Try this below thing. It should work

YESTERDAY=`TZ=GMT+24 date +%Y%m%d`; echo $YESTERDAY 
Sign up to request clarification or add additional context in comments.

1 Comment

Using this solution you might have problem if you run this batch around (-1h,+1h) midnight. It depends on the TZ, so make sure to set up your current TZ, i.e., CET or GMT+1, etc.
3

Try this one out:

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

where GMT is the time zone and you might need to alter the 24 according to the hours difference you have from GMT. Either that or you can change GMT to a time zone more comfortable to you e.g. CST

Comments

2

As larsks suggested, 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 YYYYMMDD format 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 with strftime

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

2 Comments

How can I get only the YearMonthDay from that perl script? like 20120706
I edited my answer above to include the YearMonthDay format you requestd.
2

A pure 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 

3 Comments

All the date computation strikes me as reinventing the wheel. Why not use a tool that has it built-in?
There are extra spaces in the day = last_day_of_month statement; Fix it and I'll give you a +1, especially as otherwise your solution is the only one which always gives a correct reply whatever the timezone.
@Jens, there might be a tool that has the solution built-in but then suggest it as a reply. All of the (current) remaining replies are more or less often giving an incorrect date. Reinventing the wheel might be necessary if you want a perfectly round one but find none. The accepted answer is quite a square wheel by the way ...
1

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

1 Comment

This the unique and perfect solution. It works no matter from the TZ as long as the TZ variable is correctly set. Thanks
0

Playing on Solaris10 with non-GMT environment, I'm getting this:

# 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 colud see, I have and I want to get CEST , but TZ=CEST+24 giving me wrong CEST data; GMT+24 giving me correct data, but unusable.

To get the proper result, I has to use GMT+22 (wrong command, correct result) or CEST+22 (wrong value, but finnaly correct result for correct TZ)

Comments

0

A pure bash solution given by @olivecoder is very reliable compared to any other solution but there is a mistake to be corrected. when the day fall on 1st of the month the script is failing with date saying "last_day_of_month" in day value. @olivecoder has missed $ in day=last_day_of_month, that it should be

day=$last_day_of_month; 

After this correction it works very good.

Using Timezone -24 is having some issue based on time when use it. in some cases it goes to day before yesterday. So I think its not reliable.

1 Comment

I fixed my solution as suggested. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.