Operating System - HP-UX
1753842 Members
7831 Online
108806 Solutions
New Discussion юеВ

date 5 days from now formatted at %Y-%m-%d

 
SOLVED
Go to solution
John Meissner
Esteemed Contributor

date 5 days from now formatted at %Y-%m-%d

Can anyone give a hand. I'm trying to get 5 days from now in this format
2003-10-08
i.e. %Y-%m-%d
i've used the commands
date -d "5 days next"
but it returns
Mon Oct 13 01:00:00 UTC 2003

anyone have a quick one liner (preferably without the use of caljd.sh)?

Any answer will be appreciated
All paths lead to destiny
6 REPLIES 6
Pete Randall
Outstanding Contributor

Re: date 5 days from now formatted at %Y-%m-%d

John,

That's interesting - I'm unfamiliar with the -d option. I get:

$ date -d "5 days next"
date: illegal option -- d
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]

So I'm not sure how you're going to do this without caljd.sh.

;^(


Pete


Pete
John Meissner
Esteemed Contributor

Re: date 5 days from now formatted at %Y-%m-%d

ah... sorry... linux.. wrong forum.... but if you want to work on this from a linux stand point i'd still appreciate the help
All paths lead to destiny
Jeff Schussele
Honored Contributor

Re: date 5 days from now formatted at %Y-%m-%d

Hi John,

I think you'd have a chance if you use the %j (day of the year) & increment by 5 & convert that back to a standard date. Something like...

var1=$(date +%j + 5)

but that syntax isn't right.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: date 5 days from now formatted at %Y-%m-%d

Well, you are making me do this the hard way but:

DT=$(perl -e '($y,$m,$d) = (localtime(time() + (5 * 86400))) [5,4,3]; printf("%04d-%02d-%02d",$y + 1900,$m + 1,$d);'
echo "Date = ${DT}"

I find that
DT=$(caljd.sh -y -S "-" $(caljd.sh -n 5))
echo "Date = ${DT}"
to be much simpler.

You can also download and install the Gnu version of date which will do just about what your original date requests although over a much more limited range than caljd.sh will addrsss.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: date 5 days from now formatted at %Y-%m-%d

oops, I missed a trailing paren; should be:

DT=$(perl -e '($y,$m,$d) = (localtime(time() + (5 * 86400))) [5,4,3]; printf("%04d-%02d-%02d",$y + 1900,$m + 1,$d);')
echo "Date = ${DT}"
If it ain't broke, I can fix that.
John Meissner
Esteemed Contributor

Re: date 5 days from now formatted at %Y-%m-%d

you are the man Clay! great job - thanks for the magic answer
All paths lead to destiny