bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Future date


From: Bob Proulx
Subject: Re: Future date
Date: Wed, 27 Jan 2016 13:16:19 +0800
User-agent: Mutt/1.5.24 (2015-08-30)

Val Krem wrote:
> Dave Rutherford wrote:
> > date -d 'next month' +%b%Y
> > 
> > Note, I'm not sure that's robust if you call it on, say, the 31st of the 
> > month
> > if the next month doesn't have 31 days. It might give you two months from 
> > now.
> > Read the man page for date for further enlightenment.
> 
> So easy, I am just learning about bash scripting.

Bash is the command interpreter.  There are many commands that one may
potentially invoke.  This question and discussion is really about the
commands & utilities and not so much about "bash scripting".  This
becomes more obvious as one learns more about it.  Somewhat like the
difference between cars and movies.  A car can drive you to the
theater but the two are still different things. :-)

> date -d 'next month' +%b%Y
> What would happen in December 2016. Will it give me Jan2017?

Dave was good to be concerned about this part.  Because it is often
asked.  Often enough that there is a long FAQ answer for this specific
topic.

  
https://www.gnu.org/software/coreutils/faq/coreutils-faq.html#The-date-command-is-not-working-right_002e

The specific GNU date documentation for this is here:

  
https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html

The suggestion both in the docs and the faq is to work with months
relative to the middle of the month to avoid issues of unequal months.
(And work with times from the middle of the day.)  That still isn't
absolutely perfect but is good for most calculations.  For example:

  date --date="$(date +%Y-%m-15) +1 month" +'Next month is %B.'

And so your case might best be phrased:

  date -R
    Wed, 27 Jan 2016 13:10:20 +0800

  date --date="$(date +%Y-%m-15) +1 month" +'%b%Y'
    Feb2016

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]