Convert string to date and add 1

Hi All,

I want to convert string in format YYYYMMDD(20120607) to date in unix and add 1 day to it and convert back to string in format YYYYMMDD. Please help.

If you want just tomorrow's date try this..

date --date="tomorrow" +%Y%m%d

If you want to do by addition only then try this..

#to Unix date Conversion-
$ date -d "20120607" +%s
1339041600
#add one day - add 86400 for one day..
$ expr 1339041600 + 86400
1339128000
#Get back to the previous format
$date +"%Y%m%d" -d @1339128000
20120608

Thanks for ur reply Pamu.

But I'm using SunOS and there is no -d option in my OS version. Is there any other way to do this??

Download the GNU Utilities from Coreutils - GNU core utilities

Why are you converting from string to date and back to string?

If you just want to proceed to the next date why not just increment the DD value in your YYYYMMDD string. Just add some logic to make checks depending on the MM and YY.

eg. if YY/4 = 0 && MM=02 : then last value you can have for DD is 29. so on and so forth.