Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way in unix to subtract a month or at least 30 days from a specific date? The date is to be inputted by the user.


2012-11-19 - 1 month = 2013-10-19

This one doesn't work:

date -d '2013-10-13 - 1 month' '+%F'
share|improve this question
Unfortunately, there is no portable tool to do date arithmetic. I think your best bet is to implement date arithmetic in ksh (and I know there is code on the web for that). – Gilles Oct 5 '13 at 14:39

The easiest way is to convert the date to a unix time_t value (i.e. seconds since the beginning of the epoch, or '1-1-1970 00:00:00'), and then substract 30 days * 86400 seconds per day from that number.

e.g. the following example uses set -x so that you can see the value of the D variable as it changes.

#! /bin/ksh

set -x

D=$(date -d '2013-10-13' '+%s')
D=$((D - 30 * 86400))

date -d "@$D"

Note that this depends on the GNU version of date, from GNU Coreutils. Other versions of date may not support converting a time_t value back to a formatted date with @. Unless you have installed GNU coreutils, this probably isn't available on your AIX system so gawk is probably your best option to convert back to a formatted date:

echo "$D" | gawk '{print strftime("%c", $0)}'

my original answer here suggested plain awk, but further testing on my debian system has revealed that neither mawk nor original-awk have the strftime function built-in, so you'll have to install either GNU Coreutils or GNU Awk.

If perl is installed on the system, perl can be used instead of awk:

echo $D | perl -nE 'say scalar gmtime $_'

There are several alternative methods and examples of doing this at:


share|improve this answer

Try this:

date --date="$(date -d "Nov 1 2012")-1 month" +'%Y:%m:%d'

Nov 1 2012 is used for illustration. That can be of any user input date.

share|improve this answer

Your example uses relative items in date strings which are a GNU date extension.

For example on Fedora 19 your example works as expected:

$ date -d '2013-10-13 - 1 month' '+%F'

Thus, you can make sure that your script uses GNU date.

share|improve this answer

Try this.

DAY30=`TZ=EST+720 date "+%Y-%m-%d"`

print $DAY30

720 is the offset in hours. So, 30 days is 720 hours.

EST is my time zone. I assume that you can use whatever time zone is appropriate.

share|improve this answer
What system allows offsets greater than 99:59:59? – Stéphane Chazelas Nov 27 '15 at 17:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.