# BBC BASIC Programmers' Reference

### Sidebar

finding_20the_20iso_20week_20number

## Finding the ISO week number

by Richard Russell, November 2015

The code listed below finds the ISO week number (or year and week) corresponding to a given date:

```        INSTALL @lib\$+"DATELIB"

PRINT FN_ISOweek(1,1,2005)
PRINT FN_ISOweek(2,1,2005)
PRINT FN_ISOweek(31,12,2005)
PRINT FN_ISOweek(31,12,2006)
PRINT FN_ISOweek(1,1,2007)
PRINT FN_ISOweek(30,12,2007)
PRINT FN_ISOweek(31,12,2007)
PRINT FN_ISOweek(1,1,2008)
PRINT FN_ISOweek(28,12,2008)
PRINT FN_ISOweek(29,12,2008)
PRINT FN_ISOweek(30,12,2008)
PRINT FN_ISOweek(31,12,2008)
PRINT FN_ISOweek(1,1,2009)
PRINT FN_ISOweek(31,12,2009)
PRINT FN_ISOweek(1,1,2010)
PRINT FN_ISOweek(2,1,2010)
PRINT FN_ISOweek(3,1,2010)

REM FOR d% = 1 TO 30
REM PRINT d%, FN_ISOweek(d%,11,2015)
REM NEXT
END

DEF FN_ISOweek(day%,month%,year%)
LOCAL iyr%, mjd%
iyr% = year%
mjd% = FN_mjd(day%,month%,iyr%)
IF mjd% >= FN_ISOwk1(iyr%+1) THEN iyr% += 1
IF mjd% < FN_ISOwk1(iyr%) THEN iyr% -= 1
= (mjd% - FN_ISOwk1(iyr%)) DIV 7 + 1

DEF FN_ISOwk1(year%)
LOCAL dow%, mjd%
mjd% = FN_mjd(4,1,year%)
dow% = FN_dow(mjd%)
IF dow% = 0 dow% = 7
= mjd% - dow% + 1```

If you want to know the ISO year as well as the week, you can return it in an additional parameter:

```        DEF FN_ISOweek(day%,month%,year%,RETURN iyr%)
LOCAL mjd%
... REMainder as above```