How to get the week number in a month

April 9th, 2015
by admin

Recently I wanted to display a number of week in a month. At first I wanted to get the formula from google instead of thinking, but there was no such formula. Also, I’ve found a lengthy discussion and only some indian guy gave the formula (which was incorrect of course:D)

Suppose that November the 1st is Thursday. Then for example, November 2nd is a 1st week of November, November 16th is a 3rd week, 30th is 5th week (basing on a fact that Monday starts the week)
Here’s the table that illustrates it:

​How to get the week number?

~~( ( m - w + 6 ) / 7 )

where m is an integer, a month day, 0 or 1-based
w is an integer, a week day, 0 or 1-based (e.g. Monday = 1, Thursday = 4)

Double bitwise negation is just a conversion to integer, you can use floor if you don’t care about speed (I guess that’s the case in times of virtual machine driven emulators and regex-based [greets angular;) ] js frameworks).

I’ll rebelliously leave it without a proof.

Posted in flash experiments | Comments (0)

No comments yet

Leave a Reply