Operating System - HP-UX
1833875 Members
2879 Online
110063 Solutions
New Discussion

Re: How to get previous dates from date command

 
Ezhilarasan
Occasional Advisor

How to get previous dates from date command

Hi,

How to get previous dates from date
command ? The requirement is to get any
previous dates with formats avail in
date command. Eg, if I pass parameter 0,
then today date, if -1, yesterday, if -2,
day before yesterday. If +2, then day after
tomorrow date.

Thanks
R. Ezhil
12 REPLIES 12
RAC_1
Honored Contributor

Re: How to get previous dates from date command

YESTERDAY=$(TZ=$(date +%Z)+24; date '+%b %e')
There is no substitute to HARDWORK
Dietmar Konermann
Honored Contributor

Re: How to get previous dates from date command

You may use the GNU date command. It supports things like:

date -d "5 days ago"
date -d "3 days"
date -d "3 months 2 days 6 hours ago"

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
James R. Ferguson
Acclaimed Contributor

Re: How to get previous dates from date command

Hi:

The use of the following is commonly seen to compute yesterday's date:

# TZ=GMT+24 date +%m/%d/%y

If you are in Europe, living around the Prime Meridian you're in luck. There, you can use +-24 hours as an to exactly compute yesterday or tommorrow including the correct time.

The man pages for 'environ (5)' note that is the value that must be added to local time to arrive at UTC (GMT). The offset takes the format hh[:mm[:ss]], where (hh) is any value from 0 through 23. The optional minutes (mm) and seconds (ss) fields are a value from 0 through 59. The hour field is required. If offset is preceded by a -, the time zone is east of the Prime Meridian. A + preceding the offset indicates that the time is west of the Prime Meridian.

Notice, for example, that while it is now 0730 hours on January 17 in the Eastern USA (EST), you cannot produce a date *and time* exactly 24-hours ago. To affect this, the computation would need to offset 29 hours (24+5); an invalid offset.

Clearly there are times during the day when "today" and "yesterday" would yield the same day. Caveat emptor.

Regards!

...JRF...

A. Clay Stephenson
Acclaimed Contributor

Re: How to get previous dates from date command

How about this? :

2 days previous in
16-January-2003 format

DT=$(caljd.sh -e -O -S "-" $(caljd.sh -p 2))
echo "${DT}"

8 days from now in 01 25 2003 format

DT=$(caljd.sh $(caljd.sh -n 8))
echo "${DT}"

And this works over a much wider range of dates than the Gnu date command.

Invoke as caljd.sh -u for many options and examples.

If it ain't broke, I can fix that.
Dietmar Konermann
Honored Contributor

Re: How to get previous dates from date command

And what about this small C proggie? :-) Takes a day count (positive or negative) and gives the date. OK, it does not consider leap years or DST, but nevertheless.

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)

Re: How to get previous dates from date command

Hi,

Independently of the Time Zone you actually use, you can use the following sentence to get yesterday's date:

YESTERDAY="$( export TZ=$( date +%Z )+24 ; date )"

date +%Z gives the actual timezone used on the system. +24 makes a shift of 24 hours. setting the TZ environment variable to this, "date" will give you yesterday's date.

You'll just have to compute the shift (in hours) you put in the TZ to get the result you expect:

-( 24 * 0 ) = today,
-( 24 * -1 ) = yesterday,
-( 24 * -2 ) = the day before yesterday,
...
-( 24 * +1 ) = tomorrow,
-( 24 * +2 ) = the day after tomorrow,
and so on...

After, use date command option to get the result in the format you want.

regards,
Alex
Ezhilarasan
Occasional Advisor

Re: How to get previous dates from date command

Alexandre Dumont,

Thanks for your answer for my requirement.
I am sorry for late reply.
Your one line command seems to be easy.
As you said, I tried the following to get
date for fews days pervious and later date.
It is giving wrong result.
Please correct me if wrong.

date
result--> Mon Feb 3 23:31:46 EST 2003

outdate="$( export TZ=$( date +%Z )+(24*-3); date )"

echo $outdate
result--> Mon Feb 3 23:31:46 EST 2003

outdate="$( export TZ=$( date +%Z )+72; date )"

echo $outdate
result--> Mon Feb 3 23:31:46 EST 2003


Thanks
R. Ezhil

Scott Van Kalken
Esteemed Contributor

Re: How to get previous dates from date command

I use a C program similar to the one posted here and it's fantastic.
Ezhilarasan
Occasional Advisor

Re: How to get previous dates from date command

Scott van,

Thanks for your reply.
Can you please attach your C program ?
I appreciate your help.

BTW, is there any good Web site for
Unix-C for technical knowledge sharing
and forums ?

Thanks
R. Ezhil
Brent W. Moll
Advisor

Re: How to get previous dates from date command

Here is a shell script we use called "sdate" Hopefully it will help you :)
Gary Yu
Super Advisor

Re: How to get previous dates from date command

Hi all,

I'm also interested in this topic, and I tried to reset TZ to get tomorrow and yesterday, but it seemed that this method doesn't work for offset more than 24 ours, i.e, $TZ-24 gives me tomorrow, and $TZ+24 gives me yesterday, BUT, $TZ+72 gives me today again! any idea ?
Dietmar Konermann
Honored Contributor

Re: How to get previous dates from date command

The TZ trick is not a general solution... it only works under certain conditions.

With your +72 offset the TZ is likely to be ignored completely. :-)

See environ(5). The offset needs to be in the 0..23 range.

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)