1

我必须每天执行一个批处理文件,以格式返回最后一个工作日的日期DD/MM/YYYY

我试过这个解决方案

@echo off
rem AddBusinessDays.bat date numOfDays 
rem Antonio Perez Ayala

rem Convert the date to Julian Day Number + number of days 
for /F "tokens=1-3 delims=/" %%a in ("%1") do (
  set /A mm=10%%a %% 100, dd=10%%b %% 100, yy=%%c 
)
set /A a=mm-14, jd=(1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075+2+%2, dow=jd%%7

rem Adjust Julian Day Number to avoid weekends 
if %dow% lss 2 set /A jd+=2-dow

rem Convert Julian Day Number back to date 
set /A l=jd+68569-2,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447,dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yy=100*(n-49)+i+l

rem Assemble the result 
if %dd% lss 10 set dd=0%dd% 
if %mm% lss 10 set mm=0%mm% 
set newDate=%dd%%mm%%yy% 
echo %newDate%

但我应该手动写日期

4

2 回答 2

4

此代码旨在获取昨天的日期,但如果是星期六或星期日,则它将返回上一个星期五。

@echo off
set qty=-1
:loop4weekends
set "separator="
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,now)
echo>>"%temp%\%~n0.vbs" d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)^&_
echo>>"%temp%\%~n0.vbs"         d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%" & set "MM=%result:~4,2%" & set "DD=%result:~6,2%" & set "daynum=%result:~-1%"
:: if the daynum is a weekend then loop to get the friday
set "weekend="
echo %daynum%
if %daynum% EQU 1 set weekend=1&set "qty=-3"
if %daynum% EQU 7 set weekend=1&set "qty=-2"
if defined weekend goto :loop4weekends

set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
pause
于 2013-11-11T14:46:01.040 回答
1

这个 VBS 脚本有一个修改,如果选择的日期是星期六或星期日,它将返回上一个星期五:

:: Date backward and skipping weekends
@echo off
if "%~2"=="" (
echo to get yesterdays date use     call "%~n0" today -1
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using yesterdays date
echo     call "%~n0" today -1 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)^&_
echo>>"%temp%\%~n0.vbs"         d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%" & set "MM=%result:~4,2%" & set "DD=%result:~6,2%" & set "daynum=%result:~-1%"
:: if the daynum is a weekend then run the batch file again to get the friday
set "weekend="
if %daynum% EQU 1 set /a weekend=qty - 2
if %daynum% EQU 7 set /a weekend=qty - 1
if defined weekend %0 %1 %weekend%

set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%
echo.
echo daynum is "%daynum%"
echo daynum=1 Sunday 
echo daynum=2 Monday 
echo daynum=3 Tuesday 
echo daynum=4 Wednesday 
echo daynum=5 Thursday 
echo daynum=6 Friday 
echo daynum=7 Saturday 

这是获取昨天日期的 VBS 脚本

:: Date forward & backward
@echo off
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: for this format YYYY-MM-DD using today's date
echo     call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)^&_
echo>>"%temp%\%~n0.vbs"         d
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& (
set "YY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "daynum=%result:~-1%"
)
if %daynum% EQU 1 set "weekday=Sunday"
if %daynum% EQU 2 set "weekday=Monday"
if %daynum% EQU 3 set "weekday=Tuesday"
if %daynum% EQU 4 set "weekday=Wednesday"
if %daynum% EQU 5 set "weekday=Thursday"
if %daynum% EQU 6 set "weekday=Friday"
if %daynum% EQU 7 set "weekday=Saturday"

set "day=%YY%%separator%%MM%%separator%%DD%"
echo %%day%% is set to "%day%" (without the quotes)
echo %%YY%% is set to %YY%
echo %%MM%% is set to %MM%
echo %%DD%% is set to %DD%
echo The weekday turns out to be: %weekday%
pause
于 2013-10-03T16:56:53.173 回答