When I try to get a month before last from today, I run “date –date ‘2 months ago’”.
But I run this command on Jan 31st, it return December.
I think it should return November.
Would you know how to get a month before last with date command?
Added at 17:30 (JST):
If this date calculus written in Python, I will use dateutil and it’s expression is natural as below:
from datetime import *; from dateutil.relativedelta import *
today = date.today()
twomonthago = today + relativedelta(months=-2)
So, I wonder how could I write same manner in bash shell.
Added at 18:05 (JST):
It seems that not only past month, but future month handling is same oddness.
$ date --date '+1 month'
Fri Mar 2 17:42:37 JST 2012
$ date --date '+2 month'
Sat Mar 31 17:42:56 JST 2012
$ date --date '+3 month'
Tue May 1 17:42:59 JST 2012
February and April are missing.
When you ask for “2 months ago” on the 31st of January, it is giving you the 31st of November – which doesn’t exist, so it wraps to the 1st of December.
You will have to manipulate just the month portion if that is what you want to do. This will show the month two prior as a number:
and this will format it as a month name: