2012-08-09 16 views
6

so come ottenere la data odierna in Windows 7. Qui è il comando che sto usando:come ottenere la data di ieri in un file batch

%DATE:~6,4%%DATE:~3,2%%DATE:~0,2% 

Ma io voglio ottenere ieri, non lo so Come.

+0

http://www.robvanderwoude.com/datetimentmath.php – craig65535

+1

secondo collegamento quando googling 'dos ieri date': http://www.binbert.com/blog/2010/07/previous-yesterdays- data-in-dos-batch-file/ – SeanC

+0

1 ° collegamento quando il file batch di sottrazione data googling: http://stackoverflow.com/questions/355425/date-arithmetic-in-dos-scripting – Andrew

risposta

6

Trovato uno script che funzionerà per assicurarsi di ottenere il giorno precedente anche se l'anno o il mese cambia Dos Yesterday Batch.

@echo off 

set yyyy= 

set $tok=1-3 
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u 
if "%$d1:~0,1%" GTR "9" set $tok=2-4 
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u 
set %%y=%%v 
set %%z=%%w 
set $d1= 
set $tok=)) 

if "%yyyy%"=="" set yyyy=%yy% 
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100 

set CurDate=%mm%/%dd%/%yyyy% 
set dayCnt=%1 

if "%dayCnt%"=="" set dayCnt=1 

REM Substract your days here 
set /A dd=1%dd% - 100 - %dayCnt% 
set /A mm=1%mm% - 100 

:CHKDAY 
if /I %dd% GTR 0 goto DONE 
set /A mm=%mm% - 1 
if /I %mm% GTR 0 goto ADJUSTDAY 
set /A mm=12 
set /A yyyy=%yyyy% - 1 

:ADJUSTDAY 
if %mm%==1 goto SET31 
if %mm%==2 goto LEAPCHK 
if %mm%==3 goto SET31 
if %mm%==4 goto SET30 
if %mm%==5 goto SET31 
if %mm%==6 goto SET30 
if %mm%==7 goto SET31 
if %mm%==8 goto SET31 
if %mm%==9 goto SET30 
if %mm%==10 goto SET31 
if %mm%==11 goto SET30 
REM ** Month 12 falls through 

:SET31 
set /A dd=31 + %dd% 
goto CHKDAY 

:SET30 
set /A dd=30 + %dd% 
goto CHKDAY 

:LEAPCHK 
set /A tt=%yyyy% %% 4 
if not %tt%==0 goto SET28 
set /A tt=%yyyy% %% 100 
if not %tt%==0 goto SET29 
set /A tt=%yyyy% %% 400 
if %tt%==0 goto SET29 

:SET28 
set /A dd=28 + %dd% 
goto CHKDAY 

:SET29 
set /A dd=29 + %dd% 
goto CHKDAY 

:DONE 
if /I %mm% LSS 10 set mm=0%mm% 
if /I %dd% LSS 10 set dd=0%dd% 

REM Set IIS and AWS date variables 
set IISDT=%yyyy:~2,2%%mm%%dd% 
set AWSDT=%yyyy%-%mm%-%dd% 
3
@echo off 
:: Strip the day of the week from the current date 
FOR %%A IN (%Date%) DO SET Today=%%A 
:: Parse the date, prefix day and month with an extra leading zero 
FOR /F "tokens=1-3 delims=/" %%A IN ("%Today%") DO (
    SET Day=0%%A 
    SET Month=0%%B 
    SET Year=%%C 
) 
:: Remove excess leading zeroes 
SET Day=%Day:~-2% 
SET Month=%Month:~-2% 
:: Display the results 
SET Day 
SET Month 
SET Year 
:: Convert to Julian date 
CALL :JDate %Year% %Month% %Day% 
:: Display the result 
SET JDate 
:: Subtract 1 day 
SET /A JPast = JDate - 1 
:: Display the result 
SET JPast 
:: Convert back to "normal" date again 
CALL :GDate %JPast% 
:: Display the result 
::SET GDate=20130121 
SET GDate 

echo The previous day in form YYYYMMDD is %GDate% 

pause 
:::::::::::::::::::::::::::::::::::::::::::::::::::::: 

GOTO:EOF 

:JDate 
:: Convert date to Julian 
:: Arguments : YYYY MM DD 
:: Returns : Julian date 
:: 
:: First strip leading zeroes 
SET MM=%2 
SET DD=%3 
IF %MM:~0,1% EQU 0 SET MM=%MM:~1% 
IF %DD:~0,1% EQU 0 SET DD=%DD:~1% 
:: 
:: Algorithm based on Fliegel-Van Flandern 
:: algorithm from the Astronomical Almanac, 
:: provided by Doctor Fenton on the Math Forum 
:: (http://mathforum.org/library/drmath/view/51907.html), 
:: and converted to batch code by Ron Bakowski. 
SET /A Month1 = (%MM% - 14)/12 
SET /A Year1 = %1 + 4800 
SET /A JDate = 1461 * (%Year1% + %Month1%)/4 + 367 * (%MM% - 2 -12 * %  Month1%)/12 - (3 * ((%Year1% + %Month1% + 100)/100))/4 + %DD% - 32075 

SET Month1= 
SET Year1= 
GOTO:EOF 
:GDate 
:: Convert Julian date back to "normal" Gregorian date 
:: Argument : Julian date 
:: Returns : YYYY MM DD 
:: 
:: Algorithm based on Fliegel-Van Flandern 
:: algorithm from the Astronomical Almanac, 
:: provided by Doctor Fenton on the Math Forum 
:: (http://mathforum.org/library/drmath/view/51907.html), 
:: and converted to batch code by Ron Bakowski. 
:: 
SET /A P  = %1 + 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 GYear = 100 * (%Q% - 49) + %S% + %V% 
SET /A GMonth = %U% + 2 - 12 * %V% 
SET /A GDay = %T% - 2447 * %U%/80 
:: Clean up the mess 
FOR %%A IN (P Q R S T U V) DO SET %%A= 
:: Add leading zeroes 
IF 1%GMonth% LSS 20 SET GMonth=0%GMonth% 
IF 1%GDay% LSS 20 SET GDay=0%GDay% 
:: Return value 
:: Here you can define the form that you want 
SET GDate=%GYear%%GMonth%%GDay% 
GOTO:EOF 
9

Se sei limitato a solocmd.exe, poi le altre soluzioni, nonostante le loro dimensioni, sono probabilmente buono come si arriva. Tuttavia, Windows moderno (come il tuo Win7) viene fornito con un bel po 'di strumentiche possono fare il lavoro molto più facile.

Basta creare un yester.vbs script di VBScript come segue:

d = date() - 1 
wscript.echo year(d) * 10000 + month(d) * 100 + day(d) 

Poi si può chiamare dal vostro script cmd con:

for /f %%a in ('cscript //nologo yester.vbs') do set yesterday=%%a 

e la variabile yesterday verrà creato nella forma yyyymmdd per per manipolare come desideri.

1

Ecco una soluzione che crea il file earlyday.vbs al volo, lo utilizza e lo elimina in seguito.

memorizza il risultato nella variabile NewDate

Questo esempio calcola 1 giorno fa, ma si può facilmente calcolare una data più indietro cambiando il valore della variabile offset.

@echo off 
set Offset=1 

echo d = date() - WScript.Arguments.Item(0) > earlierday.vbs 
echo wscript.echo year(d) * 10000 + month(d) * 100 + day(d) >> earlierday.vbs 

for /f %%a in ('cscript //nologo earlierday.vbs %Offset%') do set NewDate=%%a 

del earlierday.vbs  
echo %NewDate% 
pause 

Si potrebbe definire questa un po 'utilizzando% temp% \ earlierday.vbs per creare il file nella cartella Temp dell'utente.

Crediti a paxdiablo in quanto si tratta di un semplice tweak sul suo post precedente.

MODIFICA: Ecco qualcosa con un ciclo, vicino a quello che effettivamente ho bisogno di fare. Ciò richiederà 14 giorni di ferie nella data odierna e restituirà tale data. Quindi continuerà a tornare indietro 7 giorni alla volta fino a quando non arriva a 35 giorni giorno fa.

@echo off 
SETLOCAL EnableDelayedExpansion 

set BackDaysFrom=14 
Set BackDaysTo=35 
Set BackDaysStep=7 

echo d = date() - WScript.Arguments.Item(0) > earlierday.vbs 
echo wscript.echo year(d) * 10000 + month(d) * 100 + day(d) >> earlierday.vbs 

for /L %%i in (%BackDaysFrom%, %BackDaysStep%, %BackDaysTo%) do (
    for /f %%a in ('cscript //nologo earlierday.vbs %%i') do set NewDate=%%a 
    echo !NewDate! 
) 

del earlierday.vbs  

pause 
Problemi correlati