Oke, ini usahaku. Ini mencuri ide dari jawaban lain, dan upaya untuk membuat logika lebih mudah diikuti. Ini didasarkan pada sistem ISO 8601, jadi itu tidak benar jika Anda tinggal di negara-negara seperti Amerika Serikat atau Kanada, tetapi harus mudah disesuaikan untuk negara-negara tersebut.
# sets $week_start to a representation of Monday of the given week
# number formatted via the given format, and similarly sets
# $week_end to Friday of the same week.
get_week_range () {
week_num="$1" date_format="$2"
# Most of the world adhere to ISO 8601 which states that weeks begin on Monday
# and Jan 4th is always in week #1:
#
# http://en.wikipedia.org/wiki/ISO_week_date
#
# For other week numbering systems (e.g. USA, Canada), see:
#
# http://en.wikipedia.org/wiki/Seven-day_week#Week_numbering
day_in_week_1=$( date +'%Y-01-04' )
day_num_in_week_1=$( date -d $day_in_week_1 +%u ) # 1 is Monday
days_from_week_1_start=$(( $day_num_in_week_1 - 1 ))
# This is a Monday:
start_of_week_1=$( date -d "$day_in_week_1 - $days_from_week_1_start days" +%F )
week_delta="$(( $week_num - 1 ))"
# Monday:
week_start=$( date -d "$start_of_week_1 + $week_delta weeks" +"$date_format" )
# Friday:
week_end=$( date -d "$start_of_week_1 + $week_delta weeks + 4 days" +"$date_format" )
}
%V
Urutan format yang digunakan oleh 'pengguna tidak diketahui' melaporkan angka minggu ISO.