Adding one day to a date

Hi All,

I want to add one day to a date and store it in a variable.

From GUI we are passing value (last day of the month)to $t_date.

This $t_date will give me the value like this %Y%m%d 20150531.
Now I want to add one day to this value and store it in a variable "datein".

datein should give me the first day of the next month i.e. 20150601.

I tried the below script it 's not working

#!/bin/ksh
  echo "Initialization"
  echo "str Date : $t_date"
      echo "str Date = First day of the month"
	  datein=`$t_date '+%Y%m%d'`
datein=$((datein +1))
echo "$datein"    

Please help me.

Thanks.

The value of t_date in your script will not magically become the last day in some previous month unless something explicitly sets it to that value. Nothing in the script you have shown us sets the variable t_date to any value.

The ways you can calculate relative dates vary considerably from system to system and shell to shell.

What operating system are you using?

Do you have access to a 1993 or later version of the Korn shell ( ksh93 or a ksh with a version later than 1993)?

Hi,

From GUI we are passing value (last day of the month)to $t_date.
The ksh version is Version M-11/16/88i.

From GUI we are always passing last day of the month in the format %Y%m%d.

Then I have to get the first day of the next month.

Please help me.

Thanks.

Where do you pass the last day of the month to? To a process running an shell? Is the variable exported?
What be the version of your date command?

Hi,

THis $t_date is passing from $universe tool.

Thanks.

That does not answer the questions.

Hi,

How to find the version of the date.

Thanks.

Assuming a linux system, bash or ksh, and that that t_date already contains the last day of the month:

$ echo $t_date
20150531
$ date +%Y%m%d -d @$(expr 86400 + $(date +%s -d $t_date))
20150601

This works for leap years:

$ t_date=20160228
$ date +%Y%m%d -d @$(expr 86400 + $(date +%s -d $t_date))
20160229
$ t_date=20160229
$ date +%Y%m%d -d @$(expr 86400 + $(date +%s -d $t_date))
20160301

And, if the OP doesn't have a Linux system, but does have a 1993 or later ksh , the following works correctly when $t_date expands to a YYYYMMDD representation of the last day of any month:

printf '%(%Y%m%d)T\n' "$t_date + 1 day"

And, we could all write code that will work in any POSIX conforming shell on any system if the submitter would only tell us what OS an shell is being used. But, I'm not going to waste my time doing that again until the submitter answers the simple questions I asked in post #2 in this thread:

so we can provide the help the submitter needs with a solution that will work in the submitter's environment.

1 Like