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

  • 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

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)

| 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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.