Windows console

前端 未结 6 430
刺人心
刺人心 2020-12-01 21:53

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.



        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 22:33

    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
    

提交回复
热议问题