This code finds the third Thurday after a given date.
#!/bin/bash
start=2018-04-24
thursdayOfCurrentWeek=$((4-$( date +%u --date $start )))
if [[ $thursdayOfCurrentWeek -ge 0 ]]
then
date -d "$start + $thursdayOfCurrentWeek days + 2 weeks" +%F
else
date -d "$start + $thursdayOfCurrentWeek days + 3 weeks" +%F
fi
Let's split it up:
date +%u --date $start
This returns the weekday of $start
as a number (1 = Monday, ..., 4 = Thursday).
$((4-$( date +%u --date $start )))
This calculates how many days to add to the $start
date to get the Thursday of the current week.
if [[ $thursdayOfCurrentWeek -ge 0 ]]
If the date of the Thursday is today or later this week, then add only 2 weeks to the next Thursday since it's already one of the next 3 Thursdays.
date -d "$start + $thursdayOfCurrentWeek days + 2 weeks" +%F
# or
date -d "$start + $thursdayOfCurrentWeek days + 3 weeks" +%F
Add to or subtract off the $start
date the days to get the current week's Thursday and add 2 or 3 weeks depending on when the Thursday of this week is.
If the $start
date is a Thursday and it shouldn't count as one of the three just replace the if statement by:
if [[ $thursdayOfCurrentWeek -gt 0 ]]
This is a more general answer (because the start date doesn't have to be a Friday) then what you're looking for, but it also works for your use case.
Hope this helps.
PS You don't need the $end
date.
EDIT:
If you really want the minimal version if the $start
is always Friday you can do this:
#!/bin/bash
start=2018-03-23
date -d "$start - 1 day + 3 weeks" +%F