Operating System - HP-UX
1833528 Members
2975 Online
110061 Solutions
New Discussion

Re: Adding 3 months to the output of the "date" command

 
SOLVED
Go to solution
Tommy_6
Regular Advisor

Adding 3 months to the output of the "date" command

Hi everyone,
I am running the date command with the folling options:

date "+%Y%m%d%I%M%S"

How would I generate today date in this format with 3 months added to it.

Thanks is advance!!!

Tommy
6 REPLIES 6
Jeroen Peereboom
Honored Contributor

Re: Adding 3 months to the output of the "date" command

I've seen many questions on dates, all seem to be answered by something called 'caljd'. Search the forum (top of the page) for caljd.

JP.
Steven E. Protter
Exalted Contributor

Re: Adding 3 months to the output of the "date" command

http://www.hpux.ws/merijn/caljd-2.3.sh

http://www.hpux.ws/merijn/caljd-2.2.pl

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Fred Ruffet
Honored Contributor

Re: Adding 3 months to the output of the "date" command

Using perl...

/opt/perl/bin/perl
use Time::Local 'timelocal_nocheck';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(time);
print scalar localtime timelocal_nocheck $sec,$min,$hour,$mday,$mon+3,$year;
print "\n";
--

"Reality is just a point of view." (P. K. D.)
Hein van den Heuvel
Honored Contributor
Solution

Re: Adding 3 months to the output of the "date" command

Well, with perl (as much as I like that), you would have to putz around with correcting the y/m base to get the right desired numeric output (add 1900 and such).

Given this specific request with the numeric input, and just adding monts, not days the calendar math becomes trivial. extract the month, add 3, overflow at 12 into the year.

For example in awk:

$ cat x
{
y=substr($0,1,4);
m=substr($0,5,2)+3;
if (m>12) { m=m%12; y++ };
x=substr($0,7,99);
printf("%d%02d%s\n",y,m,x);
}
$ date "+%Y%m%d%I%M%S" | awk -f x
20040708015008
$
$ date "+%Y%m%d%I%M%S"
20040408015013


just for grins!
Hein.
Francisco J. Soler
Honored Contributor

Re: Adding 3 months to the output of the "date" command

Hi Tommy, if you download the gnu date, you can do this very easily:

date --date '3 months' +%Y%m%d%I%M%S

Frank.

P.S. You can get this date version from:
http://hpux.cs.utah.edu/hppd/hpux/Gnu/coreutils-5.2.0/

Linux?. Yes, of course.
Hein van den Heuvel
Honored Contributor

Re: Adding 3 months to the output of the "date" command

btw... you need to define '3 months' more sharply.

When adding 3 months to Jan-31, do you want it to become Apr-30 or May-1, because Apr-31 does not exist (but my hack will output just that!) Ditto for Nov-29 & 30.
Does you perhaps want to add 13 weeks?
Or do you know this will be run on the first of the month?

Just thought I'd warn you...

Hein