1820318 Members
2769 Online
109623 Solutions
New Discussion юеВ

yesterday's date

 
SOLVED
Go to solution
Catherine Forget
Frequent Advisor

yesterday's date

Hi,

Is there an easy way to get yesterday's date (yyyymmdd)with the date command?
Thanks
14 REPLIES 14
Steven E. Protter
Exalted Contributor

Re: yesterday's date

A. Clay Stephenson's date program comes to mind.
http://www.hpux.ws/merijn/caldj-2.3.sh

Perl
http://www.hpux.ws/merijn/caldj-2.2.pl

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: yesterday's date

You can do tricks with the TZ environment vaiable but the behavior is only defined in the range 0-24 so if your hour offset falls outside that range the behavior may work or it may not and could break at the next patch. It might also work for some timezones and not others. If you want something that will work everywhere then:

YESTERDAY=$(caljd.sh -s -y $(caljd.sh -p 1))
echo "Yesterday was ${YESTERDAY}"

Invoke as caljd.sh -u for full usage.

If it ain't broke, I can fix that.
Hazem Mahmoud_3
Respected Contributor

Re: yesterday's date

Here is a portion of a script that I used before. I'm almost certain there's a better way of doing it, but just another option and it might give you some ideas:

chop($day = `date \"+%d\"`);
chop($month = `date \"+%m\"`);
chop($year = `date \"+%y\"`);
$prevday = $day - 1;
if ($prevday == 0) #if first of month
{
if (($month==1) || ($month==3) || ($month==5) || ($month==7) || ($month==8) || ($month==10) || ($month==12))
{
$prevday = 31;
}
else
{
$prevday = 30;
if ($month == 2)
{
$prevday = 28;
}
}
}
if ($prevday =~ /^\d{1}$/) #format day to be 2 digits
{
$prevday = "0$prevday";
}


-Hazem
Nicolas Dumeige
Esteemed Contributor

Re: yesterday's date

(with oracle : select to_char(sysdate-1,'yyyymmdd') from dual;)
All different, all Unix
Hein van den Heuvel
Honored Contributor

Re: yesterday's date



perl -e '$yesterday = localtime(time-24*60*60),print "$yesterday\n"'

perl -e '$yesterday = localtime(time-86400),print "$yesterday\n"'
H.Merijn Brand (procura
Honored Contributor

Re: yesterday's date

From the FAQ at my site:

--8<---
How do I print Yesterday?

d3:/ 102 > echo 'Yesterday'
Yesterday
d3:/ 103 > perl -le 'print scalar localtime time - 86400'

Or use A.Clay Stephenson's date hammer, which can also be used for more complicated actions from the command line, as will using the modules Date::Calc or Date::Manip with Perl.
-->8---

my HP ITRC site pages can be found at

Singapore https://www.beepz.com/personal/merijn/#FAQ
Rotterdam http://www.cmve.net/~merijn/#FAQ
Seattle http://www.hpux.ws/merijn/#FAQ

And it also hosts caldj - both versions - in the last section of the page

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Francisco J. Soler
Honored Contributor

Re: yesterday's date

Hi Catherine,
If you download the date command from gnu coreutils the yesterday's date is very easy to obtain,

date --date yesterday +%F
you get the date in yyyy-mm-dd
or
date --date yesterday %Y%m%d
you get the date in yyyymmdd

I've tried with some tricky dates like:
date --date '01 march 2004 1 day ago'
and
date --date '01 march 2003 1 day ago'
and
date --date '01 Jan 2003 1 day ago'
and so on, the result is good.

You can download the date command from gnu coreutils from the software porting center:
http://hpux.cs.utah.edu/hppd/hpux/Gnu/coreutils-5.2.0/

Frank.

Linux?. Yes, of course.
Francisco J. Soler
Honored Contributor

Re: yesterday's date

Hi again,
I've made a mistake the sentence:
date --date yesterday %Y%m%d
must be
date --date yesterday +%Y%m%d

sorry
Frank.
Linux?. Yes, of course.
V.Tamilvanan
Honored Contributor

Re: yesterday's date

Catherine Forget
Frequent Advisor

Re: yesterday's date

Hi Steven,
The link you gave me :
http://www.hpux.ws/merijn/caldj-2.3.sh
is not accessible. Is there another way to get the script?

Thanks very much
H.Merijn Brand (procura
Honored Contributor

Re: yesterday's date

Any of the other two of my site should do:

My HP ITRC site pages can be found at

Singapore https://www.beepz.com/personal/merijn/caldj-2.3.sh
Rotterdam http://www.cmve.net/~merijn/caldj-2.3.sh
Seattle http://www.hpux.ws/merijn/caldj-2.3.sh

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor
Solution

Re: yesterday's date

Sorrrrry. The file was misnamed on the server.

All links should work again in a few hours. cmve is correct again

Enjoy, Have FUN! H.Merijn [ don't forget about the etiquette and assign point to _all_ answers. 0 is better than not assigned ]
Enjoy, Have FUN! H.Merijn
RAC_1
Honored Contributor

Re: yesterday's date

YESTERDAY=$(TZ=$(date +%Z)+24; date '+%b %e')

Anil
There is no substitute to HARDWORK
H.Merijn Brand (procura
Honored Contributor

Re: yesterday's date

Sorry to A.Clay!

I have now corrected *all* misspelled references to caljd.sh, caljd.pl, caljd-2.3.sh, caljd-2.2.pl, and caljd-2.2.sh on my site

Most were spelt as caldj :(

Catherine, please do not Forget to assign points to _all_ posts. 0 (or no) points is OK, but don't leave them unassigned.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn