What’s the BSD equivalent of the linux date command for adding subtracting days/weeks/months from a given date

bashcommand line

I have a given date, such as 2013-10-31 and I need to accurately determine the next day (taking into account DST, leap years, leap minutes, timezones, etc). I have no way of knowing whether the date is at the end of a month/year/etc, and I sincerely hope not having to care about that.

In Linux, I simply type:

date1="2013-10-31"
day_after=`date -ud"$date1 1 days" +%Y-%m-%d`

echo "$day_after" => 2013-11-01

However, when I try the same in Darwin/BSD:

date1="2013-10-31"
day_after=`date -v1d -ujf"%Y-%m-%d" $date1 +%Y-%m-%d`

echo "$day_after" => 2013-10-01

How can I get BSD to do proper date calculations?

Best Answer

You must specify + in your date adjustment or BSD assumes that you are giving it an actual day of the month to use.

date1="2013-10-31"
day_after=`date -v+1d -ujf"%Y-%m-%d" $date1 +%Y-%m-%d`

echo "$day_after" => 2013-11-01