Skip to content

Instantly share code, notes, and snippets.

@dawu76
Created April 2, 2024 18:33
Show Gist options
  • Save dawu76/5b0be5cde40d23cfdbcbc16494f4c8db to your computer and use it in GitHub Desktop.
Save dawu76/5b0be5cde40d23cfdbcbc16494f4c8db to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
DATE_RANGE=$1; # 2023-05-01,2023-11-01
if [[ -z "$1" ]]; then
echo "must specify date range (format YYYY-MM-DD,YYYY-MM-DD) as 2nd argument"
exit 1
fi;
IFS=",;:" read -r START_DATE END_DATE <<< "${DATE_RANGE}";
# truncate to the nearest month any dates that will be added to array
NEXT_MONTH=$(date -jf "%Y-%m-%d" "${START_DATE}" +%Y%m01);
# leave final date as-is to include start of that month if it falls before this final date
FINAL_DATE=$(date -jf "%Y-%m-%d" "${END_DATE}" +%Y%m%d);
echo "date range = [${NEXT_MONTH}, ${FINAL_DATE}]";
MONTHS=();
while [[ ${NEXT_MONTH} -lt ${FINAL_DATE} ]]; do
MONTHS+=("${NEXT_MONTH}");
NEXT_MONTH=$(date -v +1m -jf "%Y%m%d" "${NEXT_MONTH}" +"%Y%m01");
echo "most recent incremented month: ${MONTHS[${#MONTHS[@]}-1]}";
done;
printf -v MONTHS_CONCAT '%s,' "${MONTHS[@]}"
echo "${MONTHS_CONCAT%,}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment