I want to loop and print end of month date between two dates.
I have below code but it prints day on day.
JavaScript
x
startdate='2021-01-01'
enddate='2021-04-01'
enddate=$( date -d "$enddate + 1 day" +%Y%m%d ) # rewrite in YYYYMMDD format
# and take last iteration into account
thedate=$( date -d "$startdate" +%Y%m%d )
while [ "$thedate" != "$enddate" ]; do
printf 'The date is "%s"n' "$thedate"
thedate=$( date -d "$thedate + 1 days" +%Y%m%d ) # increment by one day
done
but I want these result for:
JavaScript
startdate='2021-01-01'
enddate='2021-04-01'
20210131
20210228
20210331
thanks in advance
Advertisement
Answer
Something like this
JavaScript
year=$(date +%Y)
for month in {01..04}; {
day=($(cal -d $year-$month))
echo "$year$month${day[@]:(-1)}"
}
20210131
20210228
20210331
20210430
or with -m flag
JavaScript
year=$(date +%Y)
for month in {01..04}; {
day=($(cal -m $month $year))
echo "$year$month${day[@]:(-1)}"
}
20210131
20210228
20210331
20210430