Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using DATE to calculate a value for yesterday

Status
Not open for further replies.

desktophero

Programmer
Mar 30, 2001
33
US
Does anyone have any suggestions for using DATE to calculate yesterday's date? I am writing a script file that runs binaries. The binaries require a specific date in the command line. I think that I have tried everything (that I can think of) to format a variable as the date -1 day.

I have gone to the lengths of capturing just the day of the date, and subracting one. This of course lends itself to disaster - or a lot of script - to deal with the change of years, months, etc.

Any suggestions?

Thanks,
Jason
 
The following script should do the trick. It can be improved:
- for leap-years you'll have to change 0228 into 0229,
- the result contains a space.

Sorry, but I'm in a hurry now!

Hope this helps.

(PS: don't forget to change the [1-2] before 01/01/4000!!)



#/usr/bin/sh
date +'%m%d %Y' >today
awk 'BEGIN {FS=" "}
/[1-2]/ {month_day = $1 - 1; year = $2}
/0101/ {year = $2 - 1; month_day = 1231}
/0201/ {month_day = 0131}
/0301/ {month_day = 0228}
/0401/ {month_day = 0331}
/0501/ {month_day = 0430}
/0601/ {month_day = 0531}
/0701/ {month_day = 0630}
/0801/ {month_day = 0731}
/0901/ {month_day = 0831}
/1001/ {month_day = 0930}
/1101/ {month_day = 1031}
/1201/ {month_day = 1131}
END {printf("%.4d %.4d\n", year,month_day)}' today

 
Well, I realize this reply is probably way too late to be useful, but I wrestled with this issue for a long time and found a very short answer.

date --date="# days ago" "+%format"

Substitute the # with the number of days back you want to go from the current day.

Substitute format for whatever date format you want. Any combination of the date format should work.

Ex:

date --date="1 days ago" "+%Y%j"
date --date="1 days ago" "+%a, %e %b %Y"

--
Dave
 
Dave,

It loks great, but I do't get it to run (illegal option - --) and I don't understand it either
Where did you find this?

Philip
 
Well, upon further investigation, it looks like I have a lot of egg on my face. I'm rather embarrassed.

I use that date command on one specific HP-UX system, which is the one that I verified the command on before posting my reply to the original question.

However, it is not the date command that comes with HP-UX. It is the GNU date command from their sh_utils package. I developed a script on one of my Linux systems and moved it to one of my HP9000s. Of course the date command broke when I moved the script. I had to get the GNU date command to keep the script happy. Plus, I like the way that date command works. When I installed the GNU sh_utils, it put them in /opt/sh_utils, so my old commands where still there. It did not replace anything (on my systems, anyway. YMMV)

I downloaded the sh_utils already bundled in an HP depot from Just put in sh_utils on the Package Search field and it should come up. Just be sure to rename it with a .gz extension and then gunzip it before you fire up swinstall. I tried to add this link under the Links tab for this forum, but received an error when I tried to submit it.

Sorry for any confusion,
Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top