How to use the date command to find out what date “monday week 40” will be

date

How can I use the date command to convert something like "monday week 40" into a ISO date?

I am playing with something like this:

date --date='monday week 40' +'%Y-%m-%d'

And the date I'm searching for would be 2011-10-03.

But my problem is that this date string is not valid, so I need another approach to solve this problem.

/Thanks

Best Answer

Really ugly and probably works only with GNU date:

date -d "$( date -d "$( date +'%Y-01-01' ) +40 weeks") -$( date -d "$( date +'%Y-01-01' ) +40 weeks" +'%w' ) days+1 day" +'%Y-%m-%d'

Tested only for your 3 October example, may fail for some other cases.


Update: If you have a non eng locale you need to specify the output from the inner date to get to to work. (And %F just is YYYY-MM-DD).

date -d "$(date -d "$(date +'%Y-01-01') +40 weeks" +"%F") -$(date -d "$(date +'%Y-01-01') +40 weeks" +%w) days +1 day" +"%F"
Related Question