Date Math With Shell Script

I need to set a variable to a date that is 7 days from the current date. I need it in the format mm/dd/yy.

I’ve been using this to get the date in the right format:


set shortDate to do shell script "date +%m/%d/%y"

My question is, is there a way to add seven days to this command? I’ve done this:


set shortDate to (date (do shell script "date +%m/%d/%y")) + (7 * days)

I get the right date, but in the wrong format. I’m looking for a solution that will also include the leading zeros (like 08/05/07), the way that the first script does.
I’ve tried formating the second script, but I loose the leading zeros.

I am using this for now, but I wanted to know if it can be done with a shell script:


tell ((current date) + (7 * days)) to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
	to set shortDate to text 4 thru 5 & "/" & text 2 thru 3 & "/" & text -4 thru -1

Any suggestions would be greatly appreciated.

Thanks,
Brian

You need to do all the math in the shell script, or you’ll have to coerce to an AppleScript date (which puts you back at square one).

Try something like this:

do shell script "/usr/bin/php -r 'echo date(\"m/d/y\", strtotime(\"+1 week\"));'"

Thanks Bruce, that works perfect.

I’m so lost when it comes to shell scripts. I was thinking that it would just be adding a week to :
do shell script “date +%m/%d/%y”

Is there a good resource for learning?

Thanks again,
Brian

Brian:

Our Books & Resources section has a lot of UNIX titles for you to peruse. This book is the newest in the list, and Amazon seems to be selling a lot of them.

There are more resources available at our Links Page, with one section completely devoted to Shell Scripting and UNIX.

Good luck,