Date command bug?

Nils Kassube kassube at gmx.net
Mon Aug 31 20:24:35 UTC 2015


Johnny Rosenberg wrote:
> LastDayOfMonth=$(date -d"-1 day 1 $(date -d'next month' +%F)" +'%d')
> ”date -d'next month' +%F” returns ”2015-10-01” which at least doesn't
> lead to an error, but this is still the second way everything fails:
> Next month is September, not october!

>From "info coreutils 'Relative items in date strings'":

|    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!


Nils





More information about the ubuntu-users mailing list