UNIX Shell Script to Calculate Tomorrow’s Date


A couple of readers, upon reading this article to calculate yesterday’s date in a shell script, have requested for a similar script that can be used to calculate tomorrow’s date.

Even though such a script is quite straight forward to write, many people seem to be have problem with the leap year calculations. Anway, I have managed to come up with a script to help everyone. Read on.

If you are running Linux, it’s highly likely that you have a GNU version of the date command installed. With GNU date, you can get tomorrow’s date quite easily (without the help of any shell script) by running it as follows:

[ibrahim@anfield ~]$ date  -d "+1 day"
Thu Oct  8 06:26:25 PDT 2009

If you are running a commercial UNIX distribution instead of Linux, chances are that the date command will not support the ‘-d’ option. The script below can be used on such platforms to calculate tomorrow’s date instead. Save the following script to a file called tomorrow, chmod to 755 and copy it to a directory in your PATH.

#!/bin/sh

#
# Script to calculate tomorrow's date with custom output date format
#
# Author: ibrahim - www.digitalinternals.com
#

  # default output format, change as necessary
  defaultof="%Y%m%d"

  # check for input format, else use default format,
  # refer to 'man date' for help on format
  # script only supports %Y %m %d at the moment
  of=$defaultof
  [ $# -eq 1 ] && of="$1"

  # get today's date
  eval "`date +'y=%Y m=%m d=%d'`"

  #check for max number of days in current month
  days=31
  if [ $m -eq 4 ] || [ $m -eq 6 ] || [ $m -eq 9 ] || [ $m -eq 11 ] ; then
    days=30
  fi
  # check for leap year if feb
  if [ $m -eq 2 ]; then
    days=28
    leap1=`expr $y % 4`
    leap2=`expr $y % 100`
    leap3=`expr $y % 400`
    if [ $leap1 -eq 0 ] ; then
      if [ $leap2 -gt 0 ] || [ $leap3 -eq 0 ] ; then
        days=29
      fi
    fi
  fi

  # increment date
  if [ $d -eq $days ]; then
    d=1
    m=`expr $m + 1`
    if [ $m -eq 13 ]; then
      m=1
      y=`expr $y + 1`
    fi
  else
    d=`expr $d + 1`
  fi

  #Solaris date does not accept -d
  #date -d "$y-$m-$d" +"$of"

  eval "y=`expr $y + 0` m=`expr $m + 0` d=`expr $d + 0`"
  eval "y=`printf "%04d" $y` m=`printf "%02d" $m` d=`printf "%02d" $d`"
  echo "$of" | sed -e "s/%Y/$y/" -e s/%m"/$m/" -e "s/%d/$d/"

Below are some examples on how the script can be called.

[ibrahim@anfield ~]$ date
Wed Oct  7 06:36:45 PDT 2009

[ibrahim@anfield ~]$ tomorrow
20091008

[ibrahim@anfield ~]$ tomorrow "prefix %Y-%m-%d postfix"
prefix 2009-10-08 postfix

[ibrahim@anfield ~]$ tomorrow "%Y-%m-%d"
2009-10-08

Update (09-Nov-2010): Script modified to run on Solaris platforms

Update (07-Aug-2011): Script updated to prevent printf function from interpreting certain numbers as octal

Comments
4 Comments
  1. Thanks for this wonderful script!

    Written by Andrew McGlashan, October 8, 2009
  2. Nice little script.

    Written by Mark, October 9, 2009
  3. This script still contains the “-d” option and does not work with SUN Solaris. Runing the script as is, will produce

    date: illegal option — d
    usage: date [-u] mmddHHMM[[cc]yy][.SS]
    date [-u] [+format]
    date -a [-]sss[.fff]

    Deleting the “-d” produces “bad conversion”.

    What is to change to get the script working?

    Written by Ulrich, November 9, 2010
  4. @Ulrich, thanks for highlighting the error. I have updated and tested the script on a Solaris 10 box. The script will now need access to the “printf” and “sed” binaries.

    Written by ibrahim, November 9, 2010

Leave a Comment





XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>