Proper date -d "next monday"

1

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

Kris Pruden

Posted 2012-07-31T22:59:19.157

Reputation: 113

Time to update that antique box. Anyway, I've expanded my answer based on your additional information. – Michael Hampton – 2012-08-01T00:18:03.567

Answers

1

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)

Michael Hampton

Posted 2012-07-31T22:59:19.157

Reputation: 11 744

2

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

Rain

Posted 2012-07-31T22:59:19.157

Reputation: 2 238