6

How can I determine the (ISO 8601) week number in a Windows batch file?

Unfortunately WMIC PATH WIN32_LOCALTIME GET /FORMAT:LIST only has WeekInMonth...

I have found some very complex solutions. Is there no easier way?

Community
  • 1
  • 1
Michel de Ruiter
  • 7,131
  • 5
  • 49
  • 74

3 Answers3

7

You can use Ritchie Lawrences's Date Functions. It is maintained on Gitub. https://ritchielawrence.github.io/batchfunctionlibrary/

Here is the code.

 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
 :DateToWeek %yy% %mm% %dd% yn cw dw
 ::
 :: By:   Ritchie Lawrence, Updated 2002-11-20. Version 1.1
 ::
 :: Func: Returns an ISO 8601 Week date from a calendar date.
 ::       For NT4/2000/XP/2003.
 :: 
 :: Args: %1 year component to be converted, 2 or 4 digits (by val)
 ::       %2 month component to be converted, leading zero ok (by val)
 ::       %3 day of month to be converted, leading zero ok (by val)
 ::       %4 var to receive year, 4 digits (by ref)
 ::       %5 var to receive calendar week, 2 digits, 01 to 53 (by ref)
 ::       %6 var to receive day of week, 1 digit, 1 to 7 (by ref)
 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 setlocal ENABLEEXTENSIONS
 set yy=%1&set mm=%2&set dd=%3
 if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
 set /a dd=100%dd%%%100,mm=100%mm%%%100
 set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,Jd=153*m+2
 set /a Jd=Jd/5+dd+y*365+y/4-y/100+y/400-32045
 set /a y=yy+4798,Jp=y*365+y/4-y/100+y/400-31738,t=Jp+3,Jp=t-t%%7
 set /a y=yy+4799,Jt=y*365+y/4-y/100+y/400-31738,t=Jt+3,Jt=t-t%%7
 set /a y=yy+4800,Jn=y*365+y/4-y/100+y/400-31738,t=Jn+3,Jn=t-t%%7
 set /a Jr=%Jp%,yn=yy-1,yn+=Jd/Jt,yn+=Jd/Jn
 if %Jd% GEQ %Jn% (set /a Jr=%Jn%) else (if %Jd% GEQ %Jt% set /a Jr=%Jt%)
 set /a diff=Jd-Jr,cw=diff/7+1,wd=diff%%7,wd+=1
 if %cw% LSS 10 set cw=0%cw%
 endlocal&set %4=%yn%&set %5=%cw%&set %6=%wd%&goto :EOF
Squashman
  • 13,649
  • 5
  • 27
  • 36
3

There is no native Windows command available to CMD that can easily provide the ISO 8601 week number. In fact, dealing with date and time in any fashion is a real pain.

That is why I wrote getTimestamp.bat - a general purpose utility for doing date/time computations and formatting. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe required.

Full documentation is available from the command line by getTimestamp /?, or getTimestamp /?? if you want paged help.

The following will print the ISO 8601 week number using local date and time:

call getTimeStamp /f {isowk}

You can easily store the result in a variable

call getTimeStamp /f {isowk} /r week
echo ISO 8601 week = %week%
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

Try this in a batch file:

@For /F %%I In ('PowerShell Get-Date -UFormat %%V') Do @Echo(%%I
@Timeout -1

or in the command prompt window:

For /F %I In ('PowerShell Get-Date -UFormat %V') Do @Echo(%I
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Great idea! Whenever PowerShell is installed (since Windows 7) that is. Is the `(` after `echo` on purpose? – Michel de Ruiter Sep 08 '16 at 07:12
  • 1
    Yes, it is on purpose, though probably not needed. _(it has previously been determined to be the best character to follow echo)._ Most people use a space or decimal point, _(both can be problematic)._ I usually alternate between an opening parenthesis and an equals character. – Compo Sep 08 '16 at 08:40
  • 1
    I'm sorry to say that [-UFormat %V is not the ISO week number](https://social.technet.microsoft.com/Forums/scriptcenter/57663cae-5c0b-4718-889d-5d76cba6eb04#988faeb4-1df1-414c-96b0-204ca7f1c9d8). – Michel de Ruiter Sep 08 '16 at 10:14
  • @MicheldeRuiter, I get the same result from the Powershell code as your code. You are sorry that this is not ISO week number yet your code also fails according to dbenham. – Squashman Sep 08 '16 at 23:34
  • Today yes, try other dates: it's a mess. My code only fails for the first/last few (0-3) days of the year. – Michel de Ruiter Sep 09 '16 at 15:03