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: 

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

PatRoy
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
PatRoy
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
James R. Ferguson
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...

James R. Ferguson
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
PatRoy
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!