JSI Tip 7081. What is the ISO week number and day for a given date?

Jerold Schulman

August 20, 2003

2 Min Read
ITPro Today logo in a gray background | ITPro Today


The International Standards Organization defines that a week starts on a Monday (day 1) and that the first week of a year is the one that includes the first Thursday of that year. A year can have 52 or 53 ISO weeks.

In tip 7047, I scripted Day.bat to determine the day of a given date.

Using Day.bat, I have scripted Week.bat to determine the ISO Year, ISO Week, and ISO Day of a given date. The syntax for using Week.bat is:

Call Week Year Month Day ISOYear ISOWeek ISODay

where:

Year    is the 4 digit year of the date you wish to convert,Month   is the 1 or 2 digit month of Year,Day     is the  1 or 2 digit day of Month,ISOYear is a call directed environment variable that will contain the 4 digit ISO Year,ISOWeek is a call directed environment variable that will contain the 2 digit ISO Week,ISODay  is a call directed environment variable that will contain the 2 digit ISO Day.

NOTE: Week.bat and Day.bat were modified on 24-Aug-2003 to fix a century problem.

Week.bat contains:

@echo offif {%6}=={} @echo Syntax: Call week yyyy mm dd isoyear isoweek isoday&goto :EOFsetlocal::  Week beginning date based upon 01/01 day::        M T W T F S Sset /a century=1set /a century4=1set mdt=0001313029040302set /a yyyy=%1set mma=%2set dda=%3set /a mm=100%2%%100set /a dd=100%3%%100if %mm% LSS 10 set mma=0%mm%if %dd% LSS 10 set dda=0%dd%set dt=%yyyy%%mma%%dda%call day %yyyy% %mma% %dda% weekday daynameset /a year=%yyyy%call :W1if "%dt%" LSS "%w1date%" goto prevyearset offset=000000031059090120151181212243273304334set /a leapyear=%year%%%4if %leapyear% EQU 0 set offset=000000031060091121152182213244274305335set /a off=%mm% * 3call set monoff=%%offset:~%off%,3%%set /a monoff=1000%monoff%%%1000set /a off=%monoff% + %dd%set /a off=%off% - %minus%set /a week=%off% / 7set /a week=%week% + 1if %leapyear% EQU 0 set /a century=%yyyy%%%100if %century% EQU 0 set /a century4=%yyyy%%%400if %century% EQU 0 if %century4% NEQ 0 if %mm% GEQ 3 if %weekday% EQU 7 set /a week=%week% - 1if %week% lss 10 set week=0%week%if %week% GTR 52 goto nextyearendlocal&set %4=%yyyy%&set %5=%week%&set %6=%weekday%goto :EOF:W1call day %year% 01 01 daynumb daynameset /a offset=%daynumb% * 2call set w1mon=%%mdt:~%offset%,2%%set w1date=%year%01%w1mon%if "%w1mon%" GTR "04" goto :W2set /a minus=100%w1mon%%%100goto :EOF:W2set /a lastyear=%year% - 1set /a minus=%w1mon% - 31set w1date=%lastyear%12%w1mon%goto :EOF:prevyearset /a year=%yyyy% - 1call :W1set /a monoff=365set /a leapyear=%year%%%4if %leapyear% EQU 0 set /a monoff=366set /a off=%monoff% + %dd%set /a off=%off% - %minus%set /a week=%off% / 7set /a week=%week% + 1if %leapyear% EQU 0 set /a century=%year%%%100if %century% EQU 0 set /a century4=%year%%%400if %century% EQU 0 if %century4% NEQ 0 if %weekday% EQU 7 if %week% EQU 53 set /a week=%week% - 1endlocal&set %4=%year%&set %5=%week%&set %6=%weekday%goto :EOF:nextyearset /a year=%yyyy% + 1call :w1if "%dt%" LSS "%w1date%" endlocal&set %4=%yyyy%&set %5=%week%&set %6=%weekday%&goto :EOFset week=01endlocal&set %4=%year%&set %5=%week%&set %6=%weekday%

NOTE: See tip 7090 How do I convert an ISO year, week, and day to a calendar date?



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like