11

On the Linux CLI, is there a way to get the number of the week of the month? Maybe there is another way to get this with one simple (like date) command? Let's say that day 1 to 7 is the first week, day 8 to 14 is the second week, and so on.

mgorven
  • 30,036
  • 7
  • 76
  • 121
B14D3
  • 5,110
  • 13
  • 58
  • 82
  • How do you define the week of the month? The day number divided by 7 and rounded down? Number of Sundays/Mondays/whatever which have passed? – mgorven Apr 27 '12 at 05:23
  • lets say from one to 7 day its 1rst week, from 7 to 14 second week, from 14 to 21 third and from 21 to end of the month its fourth week. I know that simple bash script with date nad if is nice solution for this but im wondering if i can di that with one command without doing a script. – B14D3 Apr 27 '12 at 05:28
  • @B14D3 your "week of month" definition is pretty coarse -- what specifically are you using this for? – voretaq7 Apr 27 '12 at 05:47
  • 1
    For nothing I was reading man for date and that came to my mind (Sometimes I have so silly thoughts). So what will be a better definition ? – B14D3 Apr 27 '12 at 05:54

5 Answers5

23

The date command can't do this internally, so you need some external arithmetic.

echo $((($(date +%-d)-1)/7+1))

Edit: Added a minus sign between the % and the d

Mike S
  • 1,103
  • 5
  • 19
  • 40
mgorven
  • 30,036
  • 7
  • 76
  • 121
  • That work fine, but I dont understand why this arithmetic returns integer ? – B14D3 Apr 27 '12 at 05:50
  • 2
    Bash only does integer arithmetic. – mgorven Apr 27 '12 at 05:52
  • Wow didn't know that. Thx :D – B14D3 Apr 27 '12 at 05:56
  • I've checked your solution and it gives 5 for days >= 29 ... So it's not working the way it supposed to. – B14D3 Apr 27 '12 at 06:05
  • 4
    That's correct -- days 29+ are in the 5th week of the month. – mgorven Apr 27 '12 at 06:06
  • 5
    Sorry I dont know how to format the comment to look nicer. Anyway, great idea to implement, however if the date is 08 or 09, it will cause error: -bash: (09: value too great for base (error token is "09"). Thats is because if numerical value starts with 0, it will be intepreted as octal number by C language, so 08 09 are invalid. For me, the workaround is to change from %d to %e, %e omits the leading 0: echo $((($(date +%e)-1)/7+1)) – Shâu Shắc Sep 09 '13 at 03:55
  • If instead of **+%d** you use **+%-d** it will strip leading zero, making code work – Francesco Belladonna Sep 18 '14 at 21:27
2

You can use this:

Monday First week day

WEEKNUMBER=$(( 1 + $(date +%V) - $(date -d "$(date -d "-$(($(date +%d)-1)) days")" +%V) ))

Sunday Firs week daty

WEEKNUMBER=$(( 1 + $(date +%U) - $(date -d "$(date -d "-$(($(date +%d)-1)) days")" +%U) ))
1

Try this:

d=`date +%d` ; m=`date +%m` ; y=`date +%Y` ; cal $m $y | sed -n "3,$ p" | sed -n "/$d/{=;q;}"
Ruy Rocha
  • 201
  • 2
  • 5
1

simplifying Victor Sanchez's solution:

expr 1 + $(date +%V) - $(date +%V -d $(date +%Y-%m-01))

replace %V with %U if you want weeks starting on Sunday.

btw: had to use expr instead of $((...)) because the later doesn't seem to like numbers with leading zeroes.

faccenda
  • 11
  • 1
0

If you accept external tools in your quest, try dateutils. It's got the notion of occurrence-within-month dates, i.e. 27 Apr 2012 is the 4th Fri in Apr 2012, which just coincides with your week definition. To get that number use:

dconv 2012-04-27 -f %c
=>
  04

%c (count) is the format specifier for the occurrence-within the month. Or to be even cooler try

dconv today -f '%cth %a in %b %Y'
=>
  1st Wed in Sep 2012
hroptatyr
  • 181
  • 1
  • 4