Tom's Guide | Tom's Hardware | Tom's Games
By: Wahine
The following batch script (incorporating VB script) allows for the date components Year Month Date to be extracted without knowing the Date format in advance. It can also extract a date + or - a given number of days from today's date.
Usage - at the command prompt enter
filename to extract today's date
filename +20 to show the date 20 days from today
filename -20 to show the date 20 days ago
filename +200 to show the date 200 days from today
filename -200 to show the date 200 days ago
:: Code begins...
set newfile=%temp%\date.vbs
echo otherdate = (Date()%1)>%newfile%
echo yy = datePart("yyyy", otherdate)>>%newfile%
echo mm = datePart("m" , otherdate)>>%newfile%
echo dd = datePart("d" , otherdate)>>%newfile%
echo wscript.echo yy^&" "^&mm^&" "^&dd>>%newfile%
FOR /F "tokens=1-3" %%A in ('cscript //nologo %newfile%') do (
set year=%%A
set month=%%B
set day=%%C
)
del %temp%\date.vbs
if %month% lss 10 set month=0%month%
if %day% lss 10 set day=0%day%
echo Year-Month-Day = %year%-%month%-%day%
:: Code ends...