There is a need to get first Saturday date of current month in my script. I am able to get last Saturday date from below command

date -d 'last Saturday' +%y%m%d

But unable to get First Saturday date. Even tried date -d 'First Saturday' +%y%m%d but not getting expected result.

My requirement is for example - in current month (July) - First Saturday date (200704)

Can anyone help me in getting first Saturday date of current month


With ksh93:

$ ksh93 -c 'printf "%(%y%m%d)T\n" "first saturday"'
  • will this command also work in perl script ?
    – Sin15
    Jul 29 '20 at 15:38
  • 1
    @Sin15 of course you could call a shell command from perl, but if your requirement is to generate the date from within perl, you should ask a separate question. As-is, this question is tagged with bash, so Answerers are assuming you are calling it from a shell script. (Notice this answer specifying the ksh93 shell).
    – Jeff Schaller
    Jul 29 '20 at 16:01


Indented commands are just for better understanding.

$ target_day_id=6 # return value of date +%w; Sunday is 0
$ year_month="$(date +%Y-%m)"
$     echo $year_month
$ day_id="$(date --date="${year_month}-01" +%w)"
$     echo $day_id
$ # July, 1st 2020 was a Wednesday
$ sat1_date="${year_month}-0$(( ( (7+target_day_id-day_id) % 7 ) + 1 ))"
$     echo $sat1_date
$     # only formatting from now on
$ sat1_date_short="${sat1_date:2}"
$     echo $sat1_date_short
$ sat1_date_short_digits_only="${sat1_date_short//-/}"
$     echo $sat1_date_short_digits_only 
  • could you please explain.... Also if in future there is requirement to replace saturday with sunday .... What value should i change ?
    – Sin15
    Jul 29 '20 at 15:41
  • @Sin15 I added the result after each command so that you can see what the commands do. I had to change the code a bit in order to make it flexible. You just set the desired target day at the beginning. Jul 29 '20 at 16:19

With GNU date and a shell loop:

until [ $(date +%A -d $(date +%Y-%m-$(printf '%02d' "$day"))) = "Saturday" ]
date +%y%m$(printf '%02d' "$day")

This initializes a "day" variable to 1 to indicate the first day of the month, then loops forward day by day until the name of that day is Saturday. GNU date is required in order to use the -d flag to use a timestamp different from "now". The test for the loop breaks down like this:

  • [ $(date +%A ...) = "Saturday" ] -- compare the output of date (asking it for the current locale's full weekday name) to "Saturday"
  • date ... -d $(date +%Y-%m-$(printf '%02d' "$day")) -- give that date command a date to display (don't use the current time) with -d; that date is generated from a second, inner date command. That inner date command prints the current time in %Y-%m-(day) format, where %Y is the current year, %m is the current month, and we pass in the loop-variable 'day'. The innermost printf statement simply ensures that the day field is zero-padded to two characters.

At the end, it prints out the current year and month in your desired format, using the calculated Saturday day.

  • To avoid locale issues, I'd use $(date +%w -d ...) -eq 6n Jul 29 '20 at 16:16
  • I was torn about how to approach it -- using day-of-week is good; in the end I decided to hard-code "Saturday" from the question. I also saw a comment from them about possibly changing to "Sunday", so having an obvious string to change might be easier to adapt.
    – Jeff Schaller
    Jul 29 '20 at 16:27

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.