Getting the year, month, day

Basic types / Date and time

#the time zone of the example; without it the local one is used
export TZ=UTC

#a fixed moment keeps the output the same; for the current date take -1
moment=1700000000

#bash's printf formats all the parts in one call, read splits them
printf -v parts '%(%Y %m %d %H %M %S %u %j)T' "$moment"
read -r year month day hour minute second dayOfWeek dayOfYear <<< "$parts"
#year is 2023, month is 11, day is 14, hour is 22,
#minute is 13, second is 20
#dayOfWeek is 2: %u counts from 1 for Monday (%w from 0 for Sunday)
#dayOfYear is 318

#trap: the parts have leading zeros, "08" and "09" are wrong octal numbers;
#10# forces base 10 in arithmetic
nextMonth=$((10#$month % 12 + 1))
#nextMonth is 12

#date gives one part, %-m and %-d drop the leading zero
monthOfMarch=$(date -d "2024-03-05" '+%-m')
#monthOfMarch is 3

echo "year is $year"
echo "month is $month"
echo "day is $day"
echo "hour is $hour"
echo "minute is $minute"
echo "second is $second"
echo "dayOfWeek is $dayOfWeek"
echo "dayOfYear is $dayOfYear"
echo "nextMonth is $nextMonth"
echo "monthOfMarch is $monthOfMarch"