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


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 off
if \{%6\}==\{\} @echo Syntax: Call week yyyy mm dd isoyear isoweek isoday&goto :EOF
setlocal
::  Week beginning date based upon 01/01 day
::        M T W T F S S
set /a century=1
set /a century4=1
set mdt=0001313029040302
set /a yyyy=%1
set mma=%2
set dda=%3
set /a mm=100%2%%100
set /a dd=100%3%%100
if %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 dayname
set /a year=%yyyy%
call :W1
if "%dt%" LSS "%w1date%" goto prevyear
set offset=000000031059090120151181212243273304334
set /a leapyear=%year%%%4
if %leapyear% EQU 0 set offset=000000031060091121152182213244274305335
set /a off=%mm% * 3
call set monoff=%%offset:~%off%,3%%
set /a monoff=1000%monoff%%%1000
set /a off=%monoff% + %dd%
set /a off=%off% - %minus%
set /a week=%off% / 7
set /a week=%week% + 1
if %leapyear% EQU 0 set /a century=%yyyy%%%100
if %century% EQU 0 set /a century4=%yyyy%%%400
if %century% EQU 0 if %century4% NEQ 0 if %mm% GEQ 3 if %weekday% EQU 7 set /a week=%week% - 1
if %week% lss 10 set week=0%week%
if %week% GTR 52 goto nextyear
endlocal&set %4=%yyyy%&set %5=%week%&set %6=%weekday%
goto :EOF
:W1
call day %year% 01 01 daynumb dayname
set /a offset=%daynumb% * 2
call set w1mon=%%mdt:~%offset%,2%%
set w1date=%year%01%w1mon%
if "%w1mon%" GTR "04" goto :W2
set /a minus=100%w1mon%%%100
goto :EOF
:W2
set /a lastyear=%year% - 1
set /a minus=%w1mon% - 31
set w1date=%lastyear%12%w1mon%
goto :EOF
:prevyear
set /a year=%yyyy% - 1
call :W1
set /a monoff=365
set /a leapyear=%year%%%4
if %leapyear% EQU 0 set /a monoff=366
set /a off=%monoff% + %dd%
set /a off=%off% - %minus%
set /a week=%off% / 7
set /a week=%week% + 1
if %leapyear% EQU 0 set /a century=%year%%%100
if %century% EQU 0 set /a century4=%year%%%400
if %century% EQU 0 if %century4% NEQ 0 if %weekday% EQU 7 if %week% EQU 53 set /a week=%week% - 1
endlocal&set %4=%year%&set %5=%week%&set %6=%weekday%
goto :EOF
:nextyear
set /a year=%yyyy% + 1
call :w1
if "%dt%" LSS "%w1date%" endlocal&set %4=%yyyy%&set %5=%week%&set %6=%weekday%&goto :EOF
set week=01
endlocal&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?



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish