Windows console

2019-01-12 04:18发布

I would like to set the date in a Windows batch file to 7 days ago from today. I would like to do this in the following format.

set today=%date:~10,4%-%date:~4,2%-%date:~-7,2%

Any ideas how to subract the 7 day time delta here ?

4条回答
仙女界的扛把子
2楼-- · 2019-01-12 04:27

Ok, I needed a batch file to display the current JDAY for an DoD operations center. You can double-click the file and it will display in a CMD window. Then, press any key to exit.

Here's what I came up with:

@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
查看更多
时光不老,我们不散
3楼-- · 2019-01-12 04:35

I refactored the code of the JDate and GDate subroutines a little bit from http://www.robvanderwoude.com/datetimentmath.php.

Usage example:

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

Code:

"Date 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
查看更多
SAY GOODBYE
4楼-- · 2019-01-12 04:37

AdamEstrada asked about subtracting dates. I had a tough time subtracting two Julian dates because of the SETLOCAL in the Julian functions. I did it by calling a function.

call:sub_Julians !Julian! %Today_Julian%

:sub_Julians

set /a delta_dates=%~1-%~2

...

goto:eof ::end:age_of_EPSdate

查看更多
Melony?
5楼-- · 2019-01-12 04:47

I posted the description below in some site some time ago:


The following Batch files convert from Date to Julian Day Number an viceversa:

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%

This way, to add/subtract a number of days to a date use the following lines:

CALL DATETOJULIAN %DATE%
SET /A NEWDATE=JDN+DAYS
CALL JULIANTODATE %NEWDATE%

Regards...

Reference: http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html


You just need to adjust your date format if it is not MM/DD/YYYY.

查看更多
登录 后发表回答