Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Yesterday's date in 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'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]
like image 533
AKIWEB Avatar asked Aug 07 '12 22:08

AKIWEB


3 Answers

Try this below thing. It should work

YESTERDAY=`TZ=GMT+24 date +%Y%m%d`; echo $YESTERDAY
like image 87
arsenal Avatar answered Oct 12 '22 02:10

arsenal


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

like image 44
George Karanikas Avatar answered Oct 12 '22 02:10

George Karanikas


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

like image 35
amdn Avatar answered Oct 12 '22 00:10

amdn