I am working in Sun OS environment, I want to add a functionality to my existing unix ksh script where it allows to read a date(in %Y%m%d format) from a file and add 1 day and rewrite the same into that file. [please note: not adding day to current date instead i want to add 1 day to i/p date present in a file]. Eg:DateFile.dat 20200620 I want my script to change it to 20200621 at the end of run. However I am getting error while adding 1 day to my variable holding i/p date My code as below:

echo $ip_dte
dte_add=`TZ=AEST-24 "$ip_dte"`
echo $dte_add
  • 1
    I think the Solaris date command is not capable of doing date arithmetic. Do you have perl installed? Commented Jun 29, 2020 at 13:09
  • @glennjackman it's Solaris so some form of perl should be installed as standard (just not necessarily a recent one) Commented Jun 29, 2020 at 13:37

3 Answers 3


Using Perl:

perl -MPOSIX=strftime  -MTime::Local -lne '
    /^(\d{4})(\d{2})(\d{2})$/ or die "Cannot parse time: $_\n";
    ($year, $month, $day) = ($1, $2, $3);
    $time = timelocal 0, 0, 12, $day, $month-1, $year-1900;
    $time += 86400;  # add one day
    print strftime("%Y%m%d", localtime $time);
' datefile

If the installed perl is version 5.10 or later, you should be able to do this:

perl -MTime::Piece -MTime::Seconds -lne '
    $fmt = "%Y%m%d";
    $time = Time::Piece->strptime($_, $fmt) or die "Cannot parse time: $_\n";
    $time += ONE_DAY;
    print $time->strftime($fmt);
' datefile

You can calculate tomorrow's date fairly easily with cal, the calendar tool. This is a bash function but it wouldn't be difficult to rewrite it

tomorrow() {
    local y=$1 m=${2#0} d=${3#0}

    if cal $m $y | xargs | sed -e 's/$/ X/' -e 's/   */ /g' | grep -v " ${d#0} " >/dev/null
        [[ $((++m)) -gt 12 ]] && m=1 && ((++y))
    printf "%d %d %d\n" $y ${m#0} ${d#0}

Armed with that it's possible to derive tomorrow's date very easily

# Get today's date into $y $m $d
IFS='-' read -r y m d <<<"2020-06-29"; echo y=$y m=${m#0} d=${d#0}
y=2020 m=6 d=29

tomorrow $y $m $d
2020 6 30

Some other examples using arbitrary dates

tomorrow 2020 6 30
2020 7 1

tomorrow 2020 2 28
2020 2 29

tomorrow 2019 2 28
2019 3 1

Here's the complementary function

yesterday() {
    local y=$1 m=${2#0} d=${3#0}

    if [[ $((--d)) -eq 0 ]]
        # Go back one month and find the last day
        [[ $((--m)) -eq 0 ]] && ((--y)) && m=12
        d=$(cal ${m#0} $y | xargs | awk '{print $NF}')
    printf "%d %d %d\n" $y ${m#0} ${d#0}

If you use something like TZ=XXX-24, this uses XXX as the name of the new timezone and GMT+24 as timezone offset.

If you like to get what you like for e.g. the European summer time, you would need to specify MET-26, for European winter time MET-25.

  • Thanks for the response schily. Yes I am aware of that. The issue here is If I give TZ=xxx-24 date, it is adding 1 day to the current date in respective timezone given. However when i pass my i/p date variable(eg: TZ=xxx-24 $mydate) instead of current date, its throwing error.
    – vivek
    Commented Jun 29, 2020 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .