GNU date can't calculate next month/last month correctly

Solution Verified - Updated -

Issue

Problem while calculating month using GNU 'date' command.

When we add 1 month to today's date, e.g. 08/31/09, it comes up with 10/01/09, not 09/30/09. We expect it to increment the month by 1, and not count out N days into the future. The real issue is with how GNU 'date' command calculates the "-d 1 month" value.  When starting date is the 31st of the month, and the next month only has 30 days, it adds 31 days.  This has the side effect of skipping to the next month, as shown in above example.
What it should probably do is increment the month, and then since the next month doesn't have 31 days, it should drop the day to 30th.

Same thing will happen when you try to get "last month" by date command.

Environment

Red Hat Enterprise Linux (all versions)

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content