Operating System - HP-UX
1827313 Members
2385 Online
109961 Solutions
New Discussion

Date format within a script

 
SOLVED
Go to solution
Simon R Wootton
Regular Advisor

Date format within a script

I'm taking today's date (eg 20/07/2004) and formatting it to a filename

example :

filename=`/bin/date +%y%m%d`

Output : 040720 - CORRECT!

However, I need to create a filename which consists of the date 2 DAYS AGO - ie. 040718

How do I amend the example above to say 'today's date minus 2 days' .....

10 points for the first correct answer!
Simon
6 REPLIES 6
Fred Ruffet
Honored Contributor

Re: Date format within a script

Hi !

Take a look at this one :
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=641246

(don't want to answer the same twice :)

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Michael Tully
Honored Contributor

Re: Date format within a script

I'm not near a system right now, but there is a fantastic tool called 'caljd' by our friend Clay Stephenson. If you use the word 'caljd' in the search engine, there are a pile of examples and in many there is an attachment of the script, which is written in perl. You could write a script and call in Clay's perl script at the appropriate time.
Anyone for a Mutiny ?
Michael Tully
Honored Contributor

Re: Date format within a script

Simon, You can get the script from these links.

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

or

http://www.cmve.net/~merijn/
The bottom of the page under "Other ITRC member contibutions".

Also in this thread there is an example right up your alley.
http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=144178
Anyone for a Mutiny ?
Muthukumar_5
Honored Contributor

Re: Date format within a script

We can create using shell scipt also. Attached script will do that. Use crontab to run the script everyday after the starting of the day. dayinfo shell variable will contain 2 days back details.

I think i have seen the question today!? okie. see the shell in attachment. it will be useful.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Date format within a script

oops. fred ruffet gave that link. :-)
Easy to suggest when don't know about the problem!
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Date format within a script

This is rather easy but you will need a fairly recent version of caljd.sh (>= 2.22) to truncate to 2-digit years.

PREVDATE=$(caljd.sh -s -y -C $(caljd.sh -p 2))

echo "Two days ago was ${PREVDATE}"

This will work across month and year boundaries as well.

Here is caljd.sh, Version 2.23:


If it ain't broke, I can fix that.