为程序的参数生成yyyyMMdd值

时间:2015-09-24 13:41:07

标签: batch-file vbscript cmd jscript

我的程序需要运行两个命令行参数:start_dateend_date

我希望我的.bat文件自动生成这些值,以便start_date始终保持M天前的值,end_date将保留N的值1}}天前。它们也应该是 yyyyMMdd 格式。

E.g。如果M=10N=5和今天是2015年9月25日,我的程序应使用以下参数运行:

MyProgram.exe "start_date:20150915" "end_date:20150920"

如何仅使用批处理文件命令实现此目的?

1 个答案:

答案 0 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

set /A M=10, N=5

rem Separate current date in DD, MM and YYYY parts:
rem Modify next line accordingly to your locale date format (this one use "MM/DD/YYYY")
for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A mm=1%%a-100, dd=1%%b-100, yyyy=%%c

rem Convert today's date to Julian Day Number
call :DateToJulian %dd% %mm% %yyyy% today=

rem Subtract the given numbers of days
set /A start_date=today-M, end_date=today-N

rem Convert the new Julian Day Numbers back to dates
call :JulianToDate %start_date% start_date=
call :JulianToDate %end_date% end_date=

echo MyProgram.exe "start_date:%start_date%" "end_date:%end_date%"
goto :EOF


rem Convert a Date to Julian Day Number
:DateToJulian Day Month Year Julian=
set /A a=(%2-14)/12, %4=(1461*(%3+4800+a))/4+(367*(%2-2-12*a))/12-(3*((%3+4900+a)/100))/4+%1-32075
exit /B

rem Convert a Julian Day Number to Date in YYYYMMDD format
:JulianToDate Julian YYYYMMDD=
set /A l=%1+68569,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
set /A dd=100+l-(2447*j)/80,l=j/11,mm=100+j+2-(12*l),yyyy=100*(n-49)+i+l
set "%2=%yyyy%%mm:~1%%dd:~1%
exit /B

Reference: http://www.hermetic.ch/cal_stud/jdn.htm#comp