Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Manipulating dates in a POSIX shell (/usr/bin/sh)

Regular Advisor

Manipulating dates in a POSIX shell (/usr/bin/sh)

Hi.

I have a shell script which needs to get today's date (i.e. 20061215) and add 1 day to this date, so I'd end up having 20061216 in a variable...

In Linux's gnu date command, I have `date -d "+1 day" +"%Y%m%d"` which works perfectly. Installing GNU Date on HPUX isn't an option for me... and I've already got my hole script in shell... so Perl isn't a friendly approach...

Has anyone done this?

Thanks, Pat
4 REPLIES
Regular Advisor

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

So far, the closest I got was to issue :

date +%s

which gives me the epoch time.. so I'd end up with something like "1166123240". I could just add up "86400", which totals a day's worth, but then, how do I convert that to a date string?

thx again! Pat
Acclaimed Contributor

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

Hi PAt:

Well, if you don't want Perl, I'd use Clay Stephenson's 'caljd' date manipulation utility!

You can download it from Merijn's website:

http://mirrors.develooper.com/hpux/

Regards!

...JRF...

Acclaimed Contributor

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

Hi Pat:

I have to add that Perl *is* such a straightforward approach for your task, though:

# perl -MPOSIX=strftime -le 'print strftime("%Y%m%d",localtime(time+(86400)))'
20061216

Plus, as you can see, the syntax mimics the GNU 'date' since both use the 'strftime' C library notation.

Regards!

...JRF...
Highlighted
Regular Advisor

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

Yep.. just found an exact same solution... I had:

perl -MPOSIX -le 'print for strftime( "%Y%m%d", localtime(time + 86400) )'

which does the same.

Thanks!