yi1
yi1

Reputation: 413

Get Tuesday and Wednesday next week, with Linux date command

At a Linux shell, you can do something like:

date -d "next Tuesday"

To get next Tuesday.

My issue is this: I want to get Tuesday of NEXT WEEK. So if I'm currently on Monday, I want it to go 7 days forward to next week, then evaluate "next Tuesday". Is there a way to chain the date evaluations somehow?

To further elaborate, if I am on a Wednesday, then next week's Tuesday is just 6 days away

Upvotes: 5

Views: 5197

Answers (2)

Holloway
Holloway

Reputation: 7367

date is cleverer than you'd think

~: date -d "next tuesday"
Tue Feb  2 00:00:00 GMT 2016
~: date -d "1 week next tuesday"
Tue Feb  9 00:00:00 GMT 2016
~: 

If you want to get the Tuesday of next week you can find the start of next week, then add a day

~: date -d "1 day next monday"
Tue Feb  2 00:00:00 GMT 2016

If you want it to be slightly clear you can use

~: date -d "next Monday + 1 day"
Tue Feb  2 00:00:00 GMT 2016

Based on Charles Duffy's comments it might be worth noting on my machine

~: date --version #on RHEL6
date (GNU coreutils) 8.4
<license stuff (GPLv3)>

Upvotes: 9

chepner
chepner

Reputation: 531075

The only way to do this reliably is to first get the next "beginning of week day" (which might vary from region to region; for this I'll assume it's Sunday), then request a day 0-6 days in the future, where 0 through 6 stand in for Sunday through Saturday, respectively.

$ bow=$(date -d "next Sunday")
$ date -d "$bow + 0 days"

Upvotes: 2

Related Questions