Operating System - HP-UX
1756367 Members
3065 Online
108847 Solutions
New Discussion юеВ

How to find the date of yesterday?

 
SOLVED
Go to solution
Hooi Siew Hoong_1
Frequent Advisor

How to find the date of yesterday?

Hi guys,

I am writing a shell script that required to find yesterday's date?

Taking into account of change of month, year and leap year, I have written a code of over 30 lines just to calculate the previous date.

Is there a better way by using just a few lines of code?

Regards
Siew Hoong
6 REPLIES 6
Clemens van Everdingen
Honored Contributor
Solution

Re: How to find the date of yesterday?

Hi,

See this thread from a few days ago !

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xc0afba808b46d611abda0090277a778c,00.html

Clemens
The computer is a great invention, there are as many mistakes as ever, but they are nobody's fault !
John Carr_2
Honored Contributor

Re: How to find the date of yesterday?

Hooi Siew Hoong_1
Frequent Advisor

Re: How to find the date of yesterday?

Thank for the quick replies.

I had read some of the threads, I think the fastest and easiest way is to play around with the timezone variable.

export TZ=
date

cheers
federico_3
Honored Contributor

Re: How to find the date of yesterday?

Compile the following (fdate.c) C script (cc fdate.c -o fdate)and use the command
fdate -1 ( to know the yesterday date)
With this script you can find whatever date you need ( fdate -N )

#include
#include

main(argc, argv)
int argc;
char **argv;
{
time_t seconds;
struct tm *timeptr;

if(argc > 1)
{
seconds = time((long *)0) + atoi(argv[1])*24*60*60;
timeptr = localtime(&seconds);
fputs(asctime(timeptr), stdout);
exit(0);
}
else
exit(1);
}


Ciao,
Federico

Roger Baptiste
Honored Contributor

Re: How to find the date of yesterday?

Hi,

Yesterday=`TZ=aaa24 date +%Y%m%d`

should work fine.

-raj
Take it easy.
A. Daniel King_1
Super Advisor

Re: How to find the date of yesterday?

GNU date has a great built-in feature which allows:

$date
Wed Apr 24 14:12:17 EDT 2002
$date -d "yesterday"
Tue Apr 23 14:12:19 EDT 2002
$date -d "a year ago"
Tue Apr 24 11:12:24 EDT 2001
$date -d "3 weeks ago"
Wed Apr 3 14:12:36 EST 2002

It is available as part of the shell-utils package from:
http://hpux.cs.utah.edu/hppd/hpux/Gnu/sh_utils-2.0/

Or, you can get the source from:
http://www.gnu.org/software/shellutils/shellutils.html
Command-Line Junkie