get week of year from day of year

Viewed 28542

Given day of year, how can I get the week of year by using Bash?

3 Answers

Adding to the other answers, I'd like to point out that there are different ways of enumerating the weeks of a year. For example, ncal displays a calendar, and when you pass it the -w argument, the week numbers are also indicated:

sudo apt install ncal
ncal -3wb
      December 2021              January 2022               February 2022        
 w| Su Mo Tu We Th Fr Sa    w| Su Mo Tu We Th Fr Sa    w| Su Mo Tu We Th Fr Sa   
49|           1  2  3  4    1|                    1    6|        1  2  3  4  5   
50|  5  6  7  8  9 10 11    2|  2  3  4  5  6  7  8    7|  6  7  8  9 10 11 12   
51| 12 13 14 15 16 17 18    3|  9 10 11 12 13 14 15    8| 13 14 15 16 17 18 19   
52| 19 20 21 22 23 24 25    4| 16 17 18 19 20 21 22    9| 20 21 22 23 24 25 26   
 1| 26 27 28 29 30 31       5| 23 24 25 26 27 28 29   10| 27 28                  
                            6| 30 31

Notice that January "touches" six (Sunday through Saturday) weeks, and while there is only one day (of 2022) in this first week, it's counted as the end of week 1 in this methodology. Also notice that the last six days of December are also considered part of week 1.

Related