Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
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
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..
Highlighted
Neoma
Frequent Advisor

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

.