 # 6.8. How can I calculate the week number?

## Description

This article is from the Calendars FAQ, by Claus Tondering claus@tondering.dk with numerous contributions by others.

# 6.8. How can I calculate the week number?

If you know the date, how do you calculate the corresponding week
number (as defined in ISO-8601)?

1) Using the formulas in section 2.15.1, calculate the Julian Day
Number, J.

2) Perform the following calculations (in which the divisions are
integer divisions in which the remainder is discarded):

d4 = (J+31741 - (J mod 7)) mod 146097 mod 36524 mod 1461
L = d4/1460
d1 = ((d4-L) mod 365) + L
WeekNumber = d1/7+1

(I am very grateful to Stefan Potthast for this algorithm.)

Note that if the week number is 1, 52, or 53, the week may lie in two
different calendar years. However, the week is always considered to
lie in the year in which it is counted. Thus, 31 December of year X,
may belong to week 1 of year X+1; similarly 1 January of year X may
belong to week 52 or 53 or year X-1.

Continue to: