Hi Experts,
i have a variable in which i am giving date as user input
a=20090728
How can i addd or substract no of days from that date and get the required day.
lets say if i will add 4 days it should show 20090801
my operating system is SunOs
Thanks,
Subhendu
aigles
2
Have a look at http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html
Jean-Pierre.
---------- Post updated at 16:10 ---------- Previous update was at 16:04 ----------
With GNU date command, you can do :
$ cat date.sh
a=20090728
b=$(date --date="$a+4days" +'%Y%m%d')
echo "$a + 4days = $b"
$ ./date.sh
20090728 + 4days = 20090801
$
Jean-Pierre.
Hi,
it gives me the following error:
date: illegal option -- date=+4days
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
+ 4days =
aigles
4
Not lucky, your date command is not a GNU version, so this solution cannot be used.
Have a look to the link in my previus post (see datecalc script).
Jean-Pierre.
Here's one way to do it in Perl:
C:\>
C:\>perl -MDate::Calc -e "$x=20090728; ($y,$m,$d)=unpack(\"A4 A2 A2\",$x); ($y,$m,$d)=Date::Calc::Add_Delta_Days($y,$m,$d,4); printf(\"%4d%02d%02d\",$y,$m,$d)"
20090801
C:\>
tyler_durden