0

Hi Not sure if anyone can help me.

How can I convert current date to week number in a batch scripting?

I only know how to generate current date from my pc. %DATE%

eg. 3/16/2017 is week 11

gonzalloe
  • 313
  • 3
  • 7
  • 22

3 Answers3

3

use this:

@echo off
set "weekn="

    for /f %%W in (
        'mshta vbscript:Execute("createobject(""scripting.filesystemobject"").GetStandardStream(1).writeline(DatePart(""ww"",Now()))"^^^&close^)'
    ) do @( 
     set "weekn=%%W"
    )

echo %weekn%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • It works perfectly! But what does the sentence inside the for loop means? – gonzalloe Mar 16 '17 at 06:35
  • @lcc `mshta` is built-in tool that can open `html` files (and also the scripts there have access to the file system) with `mshta vbscript:..` or `mshta javascript:...` you can inject executable code in its title. Though usually needs nasty escapes and writing to the console is not so easy (needs `GetStandardStream(1)` and a valid pipe) it is convenient for short expressions. – npocmaka Mar 16 '17 at 06:45
  • Do you know the equivalent method for JScript? – Aacini Mar 16 '17 at 13:29
  • @Aacini - there's no in standard javascript functions but it's not a big deal to be written. – npocmaka Mar 16 '17 at 13:41
  • Not a big deal? I reviewed the 16 answers of [this question](http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php) and the 13 answers of [this one](http://stackoverflow.com/questions/7765767/show-week-number-with-javascript), and they all have comments about "this not works" or are extremely complicated! I'll try [this one](http://stackoverflow.com/a/27125580/778560): `now = new Date(); onejan = new Date(now.getFullYear(), 0, 1); week = Math.ceil( (((now - onejan) / 86400000) + onejan.getDay() + 1) / 7 )` – Aacini Mar 16 '17 at 16:35
  • @Aacini - based on [this](http://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366) - `Math.floor(Math.floor(((new Date())-(new Date((new Date()).getFullYear(), 0, 0)))/(1000 * 60 * 60 * 24))/7+1);` – npocmaka Mar 16 '17 at 17:04
  • You should take care which week definition applies to your locale (and what is the first day of the week) see [this wikipedia article](https://en.wikipedia.org/wiki/Week#Week_numbering) –  Mar 16 '17 at 23:23
  • @Aacini: using your own code and combining it with an ingenious hack found here: (https://blogs.msdn.microsoft.com/joshpoley/2008/01/15/running-jscript-in-a-cmd-file/), you can use the following batch file `@if (@Dummy == @Dummy) @then @cscript //Nologo //E:JSCRIPT "%~dpnx0" %* & @goto :eof @end now=new Date();jan1=new Date(now.getFullYear(),0,1);WScript.Echo(Math.ceil((((now-jan1)/86400000)+jan1.getDay()+1)/7));` – Filipus May 01 '17 at 21:33
3

Alternatively, call Powershell from the batch file with a simple one-liner:

@for /f %%a in ('powershell.exe get-date -UFormat %%V') do @set WeekInYear=%%a
Filipus
  • 520
  • 4
  • 12
  • 1
    Although PowerShell is _much more powerful than Batch files_, this "simple one-liner" uses the 470 KB size `powershell.exe` file, so it takes _several seconds_ (6.96 in my test) to get the same result that is obtained immediately via any other method! The advice may be: don't use PowerShell for simple, one-time calculations like this one... – Aacini Mar 16 '17 at 19:47
  • My SSD may be skewing my own results because running that batch takes less than a second on my system. But I agree with Aacini that sometimes the road to simplicity has cost! – Filipus Mar 20 '17 at 12:50
3

Pure Batch Solution. Credit to Ritchie Lawrence

@echo off & setlocal ENABLEEXTENSIONS
call :DateToWeek 2017 03 16 yn cw dw
echo/Today (in ISO 8601 Week Date format) is: %yn%-W%cw%-%dw%
pause
goto :EOF



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
: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