I am using below to get previous, current and the next month under Ubuntu 11.04:
LAST_MONTH=`date +'%m' -d 'last month'`
NEXT_MONTH=`date +'%m' -d 'next month'`
THIS_MONTH=`date +'%m' -d 'now'`
It works well until today, the last day of October, 2012 (2012-10-31)
I get below result as of now:
$ date
Wed Oct 31 15:35:26 PDT 2012
$ date +'%m' -d 'last month'
10
$ date +'%m' -d 'now'
10
$ $ date +'%m' -d 'next month'
12
I suppose the outputs should be 9,10,11 respectively.
Don’t understand why date outputs behave like this. What should be a good way to get consistant previous, current and next month instead?
The problem is that
datetakes your request quite literally and tries to use a date of 31st September (being 31st October minus one month) and then because that doesn’t exist it moves to the next day which does. Thedatedocumentation (frominfo date) has the following advice: