Using 'Date' Command to Get Previous, Current and Next Month

Using `date` command to get previous, current and next month

The problem is that date takes 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. The date documentation (from info date) has the following advice:

The fuzz in units can cause problems with relative items. For
example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:

 $ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!

Getting next month with date command?

From the GNU man pages:

The fuzz in date units can cause problems with relative items. For example, ‘2003-07-31 -1 month’ might evaluate to 2003-07-01, because 2003-06-31 is an invalid date.
To determine the previous month more reliably, you can ask for the month before the 15th of the current month

You can use a day which exists in all months:

date +"%B %Y" --date="$(date +%Y-%m-15) next month"

Result:

April 2014

How to get previous month and previous year in MonthYear format (December2020) using Linux

date accepts only one -d option. In your command date -d "2021-01-08" '+%B' -d 'last month' the first -d is ignored. Only the -d "last month" applies. Because of that, and since we have August right now, the output is July.

You probably wanted to use

date -d '2021-01-08 - 1 month' +%B%Y

which prints December2020.

If you really wanted to concat the previous month and previous year together, you could use

echo "$(date -d'2021-01-08 - 1 month' +%B)$(date -d'2021-01-08 - 1 year' +%Y)"

but that would give rather strange results:

2021-01-08 → December2020 # 1 month before input date
2021-04-30 → March2020 # 13 months before input date

How to get the last month? date +%Y%m -d '1 month ago' doesn't work on March 30

Subtract the number of days in the current month, and you will get the last day of the previous month. For example:

date +%Y-%m-%d -d "`date +%d` day ago"

results in

2017-02-28

Since you don't care about the day and only want the month, you will always get the correct month:

lastmonth=$(date +%Y%m -d "$(date +%d) day ago")

getting a previous date in bash/unix

Several solutions suggested here assume GNU coreutils being present on the system. The following should work on Solaris:

TZ=GMT+24 date +’%Y/%m/%d’

Get the date (a day before current time) in Bash

Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.

I find out I can get the previous date with little trick on timezone.

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE

python date of the previous month

datetime and the datetime.timedelta classes are your friend.

  1. find today.
  2. use that to find the first day of this month.
  3. use timedelta to backup a single day, to the last day of the previous month.
  4. print the YYYYMM string you're looking for.

Like this:

 import datetime
today = datetime.date.today()
first = today.replace(day=1)
last_month = first - datetime.timedelta(days=1)
print(last_month.strftime("%Y%m"))

201202 is printed.



Related Topics



Leave a reply



Submit