Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

DATE 'n' Days Ago or After from Current Date

SOLVED
Go to solution
Highlighted
Neoma
Frequent Advisor

DATE 'n' Days Ago or After from Current Date

Hi All,
I want to write a script to get a date 3 days (calender days) ago from current date or after from current date.

The easiest method could be to use %j in date cmd and get the day of the year (current year) in decimal format and minus (or add ) number of days "n" for which you want the date.
Now after getting this number how to get the date of thay day of the year is my problem?
I would really appreciate if someonoe can suggest other way of getting the end result?
3 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: DATE 'n' Days Ago or After from Current Date

Hi:

Use Perl;

# perl -MPOSIX -le 'print strftime "%Y-%m-%d",localtime(time-(3*60*60*24))'
2008-09-11

perl -MPOSIX -le 'print strftime "%Y-%m-%d",localtime(time+(3*60*60*24))'
2008-09-17

You can use the same formatting directives you use with the Unix 'date' command by using 'strftime':

# perl -MPOSIX -le 'print strftime "%b %d",localtime(time-(3*60*60*24))'
Sep 11

Regards!

...JRF...
Neoma
Frequent Advisor

Re: DATE 'n' Days Ago or After from Current Date

Thanx you Very much.

Great..
Neoma
Frequent Advisor

Re: DATE 'n' Days Ago or After from Current Date

.