get week of year from day of year
Using the date
command, you can show the week of year using the "%V" format parameter:
/bin/date +%V
You can tell date
to parse and format a custom date instead of the current one using the "-d" parameter:
/bin/date -d "20100215"
Then, mixing the two options, you can apply a custom format to a custom date:
/bin/date -d "20100215" +%V
If you're using GNU date
you can use relative dates like this:
$ doy=193
$ date -d "Jan 1 +$((doy -1)) days" +%U
28
This would give you a very simplistic answer, but doesn't rely on date
:
$ echo $((doy / 7))
which pays no attention to the day of week.
Here's a demonstration of the week numbering systems:
$ printf "\nDate\t\tDOW\tDOY\t%%U %%V %%W\n"; \
for d in "Jan "{1..4}" 2010" \
"Dec "{25..31}" 2010" \
"Jan "{1..4}" 2011"; \
do printf "%s\t" "$d"; \
date -d "$d" +"%a%t%j%t%U %V %W"; \
done
Date DOW DOY %U %V %W
Jan 1 2010 Fri 001 00 53 00
Jan 2 2010 Sat 002 00 53 00
Jan 3 2010 Sun 003 01 53 00
Jan 4 2010 Mon 004 01 01 01
Dec 25 2010 Sat 359 51 51 51
Dec 26 2010 Sun 360 52 51 51
Dec 27 2010 Mon 361 52 52 52
Dec 28 2010 Tue 362 52 52 52
Dec 29 2010 Wed 363 52 52 52
Dec 30 2010 Thu 364 52 52 52
Dec 31 2010 Fri 365 52 52 52
Jan 1 2011 Sat 001 00 52 00
Jan 2 2011 Sun 002 01 52 00
Jan 3 2011 Mon 003 01 01 01
Jan 4 2011 Tue 004 01 01 01