Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a quick script that figures out the date of the next Monday that isn't today. Although the date command accepts this expression, it appears to interpret it as equivalent to "this monday". That is, if today is Monday, it emits today's date, otherwise it emits next week's Monday.

Any ideas how to do this with the date command (or really any command available to bash) without resorting to doing the date arithmetic myself?

Update: This appears to be something strange with my environment, as several folks have shown the command working as expected on their systems. This is what I see:

$ date
Tue Jul 31 23:38:28 UTC 2012
$ date -d 'next tuesday'
Tue Jul 31 00:00:00 UTC 2012
$ date --version
date (GNU coreutils) 5.97

It would appear I have an ancient version of date, perhaps that's my issue...

share|improve this question
Time to update that antique box. Anyway, I've expanded my answer based on your additional information. – Michael Hampton Aug 1 '12 at 0:18
up vote 1 down vote accepted

GNU date seems to be working correctly.

error@underground ~/Downloads $ date -d "next monday"
Mon Aug  6 00:00:00 EDT 2012
error@underground ~/Downloads $ date -d "next tuesday"
Tue Aug  7 00:00:00 EDT 2012
error@underground ~/Downloads $ date -d "next wednesday"
Wed Aug  1 00:00:00 EDT 2012
error@underground ~/Downloads $ date --version
date (GNU coreutils) 8.15

Check to ensure that your system actually has the correct date, time and time zone.

Also check to make sure you're running a relatively recent version of GNU coreutils. The behavior you describe was a bug in old versions of coreutils that was fixed in version 6.0.

date: a command like date -d '2006-04-23 21 days ago' would print the wrong date in some time zones. (see the test for an example)

share|improve this answer

Are you sure there isn't a problem with how you are executing the command within the script, maybe missing quotes or something of that nature? I cannot reproduce your problem.

$ date
Tue Jul 31 19:08:50 EDT 2012
$ date -d "next tuesday"
Tue Aug  7 00:00:00 EDT 2012
$ date -d "this tuesday"
Tue Jul 31 00:00:00 EDT 2012
$ date --version
date (GNU coreutils) 8.5
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .