Skip navigation

JSI Tip 7090. How do I convert an ISO year, week, and day to a calendar date?


In tip 7081 » What is the ISO week number and day for a given date, I scripted Week.bat, which uses Day.bat to determine the ISO day number.

I have scripted WeekDate.bat to convert an ISO year, week, and day number to a calendar year, month, and day. WeekDate.bat uses Week.bat and Day.bat.

The syntax for using WeekDate.bat is:

call weekdate ISOYear ISOWeek ISODay yyyy mm dd
where:

ISOYear is the International Standards Organization year of the ISOWeek,
ISOWeek is the ISO week number, from 1 through 52, or 53,
ISODay  is the ISO day number, from 1 (Monday) through 7 (Sunday),
yyyy    is the call directed environment variable that will contain the 4 digit calendar year,
mm      is the call directed environment variable that will contain the 2 digit calendar month, and
dd      is the call directed environment variable that will contain the 2 digit calendar day of the month.
NOTE: WeekDate.bat, Week.bat, and Day.bat where modified on 24-Aug-2003 to fix a century problems.

WeekDate.bat contains:

@echo off
if \{%6\}==\{\} @echo Syntax: Call weekdate isoyear isoweek isoday yyyy mm dd&goto :EOF
setlocal
set /a isoyear=%1
set /a leapyear=%isoyear%%%4
set /a century=%isoyear%%%100
set /a century4=%isoyear%%%400
set /a isoweek=100%2%%100
set /a isoday=100%3%%100
set isoweeka=%isoweek%
if %isoweek% LSS 10 set isoweeka=0%isoweek%
set iso=%isoyear%%isoweeka%%isoday%
call week %isoyear% 01 01 J1Y J1W J1D
set J1=%J1Y%%J1W%%J1D%
set /a J1W=100%J1W%%%100
set /a J1D=%J1D%
call week %isoyear% 12 31 D31Y D31W D31D
set D31=%D31Y%%D31W%%D31D%
set /a D31W=100%D31W%%%100
set /a D31D=%D31D%
if "%iso%" LSS "%J1%" goto isoless
if "%iso%" GTR "%D31%" goto isogreat
set /a weeks=%isoweek% * 7
set /a days=%weeks% + %isoday%
if %isoyear% NEQ %J1Y% goto notsy
set /a ly=7 + %J1D%
set /a days=%days% - %ly%
goto month
:notsy
set /a days=%days% - %J1D% 
goto month
:isoless
if %isoyear% NEQ %J1Y% goto isolyr
set /a yyyy=%isoyear% - 1
set mon=12
set /a days=32 + %isoday%
set /a day=%days% - %J1D%
endlocal&set %4=%yyyy%&set %5=%mon%&set %6=%day%&goto :EOF
:isolyr
set /a days=8 - %J1D%
set /a days=%days% + %isoday%
goto month
:isogreat
if %isoyear% NEQ %D31Y% goto isonyr
set /a yyyy=%isoyear% + 1
set mon=01
set /a days=%iso% - %d31%
set day=%days%
if %days% LSS 10 set day=0%days%
endlocal&set %4=%yyyy%&set %5=%mon%&set %6=%day%&goto :EOF
:isonyr
set /a weeks=0
set /a days=365
if %leapyear% EQU 0 set /a days=%days% + 1
set /a days=%days% + %isoday%
if %isoyear% NEQ %J1Y% set /a days=%days% + 7 - %J1D%
:month
if %century% EQU 0 if %century4% NEQ 0 if %days% GTR 58 set /a days=%days% + 1
set /a days=%days% + 1
set montab=312831303130313130313031
if %leapyear% EQU 0 set montab=312931303130313130313031
set /a mon=0
:monloop
call set ms=%%montab:~%mon%,2%%
set /a ms=%ms%
if %days% LEQ %ms% goto monset
set /a days=%days% - %ms%
set /a mon=%mon% + 2
if %mon% GEQ 24 goto monset
goto monloop
:monset
set /a mon=%mon% / 2
set /a mon=%mon% + 1
if %leapyear% NEQ 0 if %mon% EQU 2 if %days% EQU 29 set /a mon=3&set /a days=1
if %mon% LSS 10 set mon=0%mon%
if %days% LSS 10 set days=0%days%
endlocal&set %4=%yyyy%&set %5=%mon%&set %6=%days%&goto :EOF



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