我想在Windows批处理文件中的日期从今天设置为7天前。 我想这样做的格式如下。
set today=%date:~10,4%-%date:~4,2%-%date:~-7,2%
任何想法如何在这里subract 7天时间差?
我想在Windows批处理文件中的日期从今天设置为7天前。 我想这样做的格式如下。
set today=%date:~10,4%-%date:~4,2%-%date:~-7,2%
任何想法如何在这里subract 7天时间差?
我张贴在一些网站下面的说明前一段时间:
以下批处理文件从日期到儒略日数的转换反之亦然:
DATETOJULIAN.BAT:
@ECHO OFF
REM CONVERT DATE TO JULIAN DAY NUMBER
REM ANTONIO PEREZ AYALA
REM GET MONTH, DAY, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524
JULIANTODATE.BAT:
REM CONVERT JULIAN DAY NUMBER TO MONTH, DAY, YEAR
REM ANTONIO PEREZ AYALA
SET /A W=(%1*100-186721625)/3652425, X=W/4, A=%1+1+W-X, B=A+1524, C=(B*100-12210)/36525, D=36525*C/100, E=(B-D)*10000/306001, F=306001*E/10000, DD=B-D-F, MM=E-1, YY=C-4716
IF %MM% GTR 12 SET /A MM-=12, YY+=1
REM INSERT LEFT ZEROS, IF NEEDED
IF %DD% LSS 10 SET DD=0%DD%
IF %MM% LSS 10 SET MM=0%MM%
REM SHOW THE DATE
ECHO %MM%/%DD%/%YY%
这样一来,加/减天数为日期使用下面几行:
CALL DATETOJULIAN %DATE%
SET /A NEWDATE=JDN+DAYS
CALL JULIANTODATE %NEWDATE%
问候...
参考: http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html
你只需要调整您的日期格式,如果它不是MM / DD / YYYY。
AdamEstrada被问及减去日期。 我有一个艰难的时间减去2 Julian日期,因为在朱利安功能SETLOCAL的。 我通过调用函数做到了。
拨打:sub_Julians朱利安! %Today_Julian%
:sub_Julians
集/一个delta_dates =%〜1-%〜2
...
GOTO:EOF ::结束:age_of_EPSdate
好吧,我需要一个批处理文件来显示的国防部行动中心目前JDAY。 您可以双击该文件,它将在CMD窗口中显示。 然后,按任意键退出。
以下是我想出了:
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
:: Call the day ordinal number subroutine
call :JDdayNumber %DD% %MM% %YYYY% DayOrdinalNumber
:: Display the result
echo.
echo Today is JDay %DayOrdinalNumber%
echo.
pause,
endlocal & goto :EOF
:: set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
:: set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
:: echo datestamp: "%datestamp%"
:: echo timestamp: "%timestamp%"
:: echo fullstamp: "%fullstamp%"
:: ============================================================
:: Subroutine: Calculate a day's ordinal number within the year
::JDdayNumber day month year return_
setlocal enableextensions enabledelayedexpansion
if %2 LEQ 2 (
set /a f=%1-1+31*^(%2-1^)
) else (
set /a a=%3
set /a b=!a!/4-!a!/100+!a!/400
set /a c=^(!a!-1^)/4-^(!a!-1^)/100+^(!a!-1^)/400
set /a s=!b!-!c!
set /a f=%1+^(153*^(%2-3^)+2^)/5+58+!s!
)
set /a return_=%f%+1
endlocal & set "%4=%return_%" & goto :EOF
我重构了的代码JDate
和GDate
子程序从一点点http://www.robvanderwoude.com/datetimentmath.php 。
Enter ISO date:
Enter number of days to add: 7
2017-01-01 + 7 days = 2017-01-08
Enter ISO date:
Enter number of days to add:
2017-01-08 + 7 days = 2017-01-15
Enter ISO date:
Enter number of days to add:
2017-01-15 + 7 days = 2017-01-22
Enter ISO date:
Enter number of days to add:
2017-01-22 + 7 days = 2017-01-29
Enter ISO date:
Enter number of days to add:
2017-01-29 + 7 days = 2017-02-05
Enter ISO date: 2017-02-12
Enter number of days to add: -7
2017-02-12 + -7 days = 2017-02-05
“日期math.bat”:
@echo off
call :main %*
goto :eof
:main
setlocal
call :initialize "2017-01-01" "1"
endlocal
goto :eof
:initialize
setlocal
set "previousDate=%~1"
set /a "numberOfDays=%~2"
set /p "previousDate=Enter ISO date: "
set /p "numberOfDays=Enter number of days to add: "
set "currentDate="
call :addIsoDateDays "%previousDate%" "%numberOfDays%" currentDate
echo %previousDate% + %numberOfDays% days = %currentDate%
echo.
call :initialize "%currentDate%" "%numberOfDays%"
endlocal
goto :eof
:stripLeadingZero
setlocal
set "number=%~1"
if %number:~0,1% equ 0 (
set "number=%number:~1%"
)
(
endlocal
set "%~2=%number%"
)
goto :eof
:addLeadingZero
setlocal
set "number=%~1"
if %number% lss 10 (
set "number=0%number%"
)
(
endlocal
set "%~2=%number%"
)
goto :eof
:gregorianToJulianDate
setlocal
set "gregorianYear=%~1"
set "gregorianMonth=%~2"
set "gregorianDay=%~3"
call :stripLeadingZero "%gregorianMonth%" gregorianMonth
call :stripLeadingZero "%gregorianDay%" gregorianDay
set /a "julianYear=(%gregorianYear% + 4800)"
set /a "julianMonth=((%gregorianMonth% - 14) / 12)"
set /a "julianDate=((1461 * (%julianYear% + %julianMonth%) / 4) + (367 * (%gregorianMonth% - 2 - (12 * %julianMonth%)) / 12) - ((3 * ((%julianYear% + %julianMonth% + 100) / 100)) / 4) + (%gregorianDay% - 32075))"
(
endlocal
set "%~4=%julianDate%"
)
goto :eof
:isoToJulianDate
setlocal
set "date=%~1"
set "year="
set "month="
set "day="
for /f "tokens=1-3 delims=-" %%a in ("%date%") do (
set "year=%%a"
set "month=%%b"
set "day=%%c"
)
set /a "julianDate=0"
call :gregorianToJulianDate "%year%" "%month%" "%day%" julianDate
(
endlocal
set "%~2=%julianDate%"
)
goto :eof
:julianToGregorianDate
setlocal
set /a "julianDate=%~1"
set /a "p=(%julianDate% + 68569)"
set /a "q=(4 * %p% / 146097)"
set /a "r=(%p% - ((146097 * %q%) + 3) / 4)"
set /a "s=(4000 * (%r% + 1) / 1461001)"
set /a "t=(%r% - ((1461 * %s%) / 4) + 31)"
set /a "u=(80 * %t% / 2447)"
set /a "v=(%u% / 11)"
set /a "gregorianYear=((100 * (%q% - 49)) + %s% + %v%)"
set /a "gregorianMonth=(%u% + 2 - (12 * %v%))"
set /a "gregorianDay=(%t% - (2447 * %u% / 80))"
call :addLeadingZero "%gregorianMonth%" gregorianMonth
call :addLeadingZero "%gregorianDay%" gregorianDay
(
endlocal
set "%~2=%gregorianYear%"
set "%~3=%gregorianMonth%"
set "%~4=%gregorianDay%"
)
goto :eof
:julianToIsoDate
setlocal
set /a "julianDate=%~1"
set "year="
set "month="
set "day="
call :julianToGregorianDate "%julianDate%" year month day
set "isoDate=%year%-%month%-%day%"
(
endlocal
set "%~2=%isoDate%"
)
goto :eof
:addIsoDateDays
setlocal
set "previousIsoDate=%~1"
set /a "numberOfDays=%~2"
set /a "previousJulianDate=0"
call :isoToJulianDate "%previousIsoDate%" previousJulianDate
set /a "currentJulianDate=(%previousJulianDate% + %numberOfDays%)"
set "currentIsoDate="
call :julianToIsoDate "%currentJulianDate%" currentIsoDate
(
endlocal
set "%~3=%currentIsoDate%"
)
goto :eof