Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am new to scripting I needed a script in the AIX environment to get the next days date based on the date entered by the user of the script.

After hard time browsing for this I got a piece of code which does the reverse of the goal which i had to achieve. It gets me one day before(Yesterday's) the date entered by the user.

The code is as below

#!/bin/ksh
echo "Enter the date (YYYY/MM/DD):"
read date
YEAR=`echo $date | cut -d"/" -f1`
MONTH=`echo $date | cut -d"/" -f2`
DAY=`echo $date | cut -d"/" -f3`
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
echo "Yesterday's Date is $YEAR/$MONTH/$DAY"

Can some one please help to me acheive this. That is to get the next day of the user entered date.

Desired Output:

Enter the date (YYYY/MM/DD): 2013/09/30

Tomorrow's Date is 2013/10/1
share|improve this question

5 Answers 5

With relatively recent versions of ksh93:

$ printf "%(%Y/%m/%d)T\n" "2014/06/20 +1 day"
2014/06/21

Or:

$ printf "%(%Y/%m/%d)T\n" "2014/06/20 next day"
2014/06/21  
$ printf "%(%Y/%m/%d)T\n" "2014/06/20 tomorrow"
2014/06/21
share|improve this answer

Using the Perl approach, with just a very basic/common modules (Time::Local and POSIX):

#!/bin/ksh
echo "Enter the date (YYYY/MM/DD):"
read date
timestamp=`perl -MTime::Local=timelocal -e '@t = split(/[-\/]/, $ARGV[0]); $t[1]--; print timelocal(1,1,1,reverse @t);' $date`
YESTERDAY=`perl -MPOSIX=strftime -e 'print strftime("%Y/%m/%d", localtime($ARGV[0] -86400));' $timestamp`
TODAY=`perl -MPOSIX=strftime -e 'print strftime("%Y/%m/%d", localtime($ARGV[0]));' $timestamp`
TOMORROW=`perl -MPOSIX=strftime -e 'print strftime("%Y/%m/%d", localtime($ARGV[0] +86400));' $timestamp`
echo "YESTERDAY= $YESTERDAY"
echo "TODAY= $TODAY"
echo "TOMORROW= $TOMORROW"

Example:

Enter the date (YYYY/MM/DD):
2014/02/28
YESTERDAY= 2014/02/27
TODAY= 2014/02/28
TOMORROW= 2014/03/01

If don't have the Time::Local module, this can still be done with a bit more of pain. However, the POSIX module (strftime method) is mandatory as being the "smart" part of the thing. Without it, it would be difficult to go from "28-feb" to "01-mar".

share|improve this answer

Pretty sure AIX does not have GNU date by default. Depending on your version of perl, you can do:

perl -MTime::Piece -MTime::Seconds -le '
    $tomorrow = Time::Piece->strptime($ARGV[0], "%Y/%m/%d") + ONE_DAY;
    print $tomorrow->ymd("/")
' 2013/10/28

So, in ksh

#!/bin/ksh
read date?"Enter the date (YYYY/MM/DD): "
tomorrow=$(
    perl -MTime::Piece -MTime::Seconds -le '
        $tomorrow = Time::Piece->strptime($ARGV[0], "%Y/%m/%d") + ONE_DAY;
        print $tomorrow->ymd("/")
    ' "$date"
)
echo "Tomorrow is $tomorrow"

Running this looks like:

Enter the date (YYYY/MM/DD): 2013/12/31
Tomorrow is 2014/01/01

Tested on an old version of ksh and perl

$ what /usr/bin/ksh
/usr/bin/ksh:
        Version M-11/16/88i
        SunOS 5.8 Generic 110662-24 Apr 2007
$ perl --version

This is perl, v5.10.0 built for sun4-solaris
share|improve this answer
    
No input validation is performed here, so if the user enters an invalid date format, perl will complain. –  glenn jackman Oct 28 '13 at 17:53
    
:I dont have perl modules installed in the machine I use. So is there any way to achieve this without perl. –  cm60 Oct 28 '13 at 17:59
    
Did you check? I believe that Time::Piece and Time::Seconds are core Perl modules. –  glenn jackman Oct 28 '13 at 18:06
    
Yes. I had tried it earlier I was getting perl modules not found errors. I do not have admin access for that machine to install the perl modules. –  cm60 Oct 28 '13 at 18:35
    
:Can you please help me to edit the above piece of code. –  cm60 Oct 28 '13 at 18:37
printf "Enter the date (YYYY/MM/DD):" ; read user_date

desired_date=`date --date="$user_date+1 day" +"%Y/%m/%d"`

echo "Tomorrow's Date is $desired_date"
share|improve this answer
    
Not on AIX you can't. This would work on Linux. –  Gilles Oct 28 '13 at 23:49

You can try the following:

currDate=$(date); nextDay=$(date -d "$currDate + 1 day" +"%Y-%m-%d"); echo "$nextDay"
share|improve this answer
    
Not on AIX you can't. This would work on Linux. –  Gilles Oct 28 '13 at 23:49
    
Oh I totally missed AIX.It would be a matter of looking at the date manual on AIX. –  val0x00ff Oct 29 '13 at 8:02
2  
Be my guest: the date manual on AIX –  Gilles Oct 29 '13 at 23:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.