Récupérer date de la veille en batch formatUS

Warrer3 Messages postés 2 Statut Membre -  
Warrer3 Messages postés 2 Statut Membre -
Bonjour à tous,

Je sais que cette question à été posée plusieurs fois, j'ai fait mes recherches et j'ai vu pas mal de résultats. Mais ne maîtrisant pas trop le batch dos, j'aimerai savoir comment je peux récupérer dans une variable, la date de la veille mais ne format US !

C'est à dire mm/dd/yyyy .

j'ai déjà quelques fonctions sous la main mais je n'arrive pas à leur faire cracher le bon format de date :

date_hier = DateAdd("d", -1, Date)

for /F "tokens=2,3,4 delims=/ " %%d in ('date /T') do (
set jour=%%d%%e%%f
)

Merci d'avance pour le service que vous me rendez !! :)
Configuration: Windows XP
Opera 9.51

1 réponse

  1. Warrer3 Messages postés 2 Statut Membre 8
     
    Pour ceux que ça intéresse, j'ai trouvé la solution et pour tous les cas possibles et envisageables !!

    :: Keep variables local
    SETLOCAL

    :: Export registry's date format settings to a temporary file
    START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International"

    :: Read the exported data
    FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B
    FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B
    DEL %TEMP%.\_TEMP.REG

    :: Remove quotes from exported values
    SET iDate=%iDate:"=%
    SET sDate=%sDate:"=%

    :: Parse today's date depending on registry's local date format settings
    IF %iDate%==0 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
    SET LocalFormat=MM%sDate%DD%sDate%YYYY
    SET YesterLocal=%%YesterM%%%sDate%%%YesterD%%%sDate%%%YesterY%%
    SET Year=%%C
    SET Month=%%A
    SET Day=%%B
    )
    IF %iDate%==1 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
    SET LocalFormat=DD%sDate%MM%sDate%YYYY
    SET YesterLocal=%%YesterD%%%sDate%%%YesterM%%%sDate%%%YesterY%%
    SET Year=%%C
    SET Month=%%B
    SET Day=%%A
    )
    IF %iDate%==2 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
    SET LocalFormat=YYYY%sDate%MM%sDate%DD
    SET YesterLocal=%%YesterY%%%sDate%%%YesterM%%%sDate%%%YesterD%%
    SET Year=%%A
    SET Month=%%B
    SET Day=%%C
    )

    :: Remove the day of week if applicable
    FOR %%A IN (%Year%) DO SET Year=%%A
    FOR %%A IN (%Month%) DO SET Month=%%A
    FOR %%A IN (%Day%) DO SET Day=%%A

    :: Today's date in YYYYMMDD format
    SET SortDate=%Year%%Month%%Day%

    :: Today's date in local format
    FOR %%A IN (%Date%) DO SET Today=%%A

    :: Strip leading zero from Day
    SET DayS=%Day%
    IF %Day:~0,1%==0 SET DayS=%Day:~1%

    :: Calculate yesterday's date
    IF %DayS% EQU 1 (
    SET YesterY=%Year%
    CALL :RollMonth
    ) ELSE (
    SET /A YesterD=%DayS% - 1
    SET YesterM=%Month%
    SET YesterY=%Year%
    )

    :: Add leading zero to YesterD if necessary
    IF %YesterD% LSS 10 SET YesterD=0%YesterD%

    :: Yesterday's date in YYYYMMDD format
    SET SortYest=%YesterM%/%YesterD%/%YesterY%

    :: Display the results
    ECHO Format: YYYYMMDD (%LocalFormat%)
    ECHO.==================================
    ECHO Today: %SortDate% (%Today%)
    CALL ECHO Yesterday: %SortYest% (%YesterLocal%)

    :: Done

    ENDLOCAL
    GOTO:EOF

    :RollMonth
    IF %Month%==01 (
    SET YesterD=31
    SET YesterM=12
    SET /A YesterY = %Year% - 1
    )
    IF %Month%==02 (
    SET YesterD=31
    SET YesterM=01
    )
    IF %Month%==03 (
    SET YesterD=28
    SET YesterM=02
    CALL :LeapYear
    )
    IF %Month%==04 (
    SET YesterD=31
    SET YesterM=03
    )
    IF %Month%==05 (
    SET YesterD=30
    SET YesterM=04
    )
    IF %Month%==06 (
    SET YesterD=31
    SET YesterM=05
    )
    IF %Month%==07 (
    SET YesterD=30
    SET YesterM=06
    )
    IF %Month%==08 (
    SET YesterD=31
    SET YesterM=07
    )
    IF %Month%==09 (
    SET YesterD=31
    SET YesterM=08
    )
    IF %Month%==10 (
    SET YesterD=30
    SET YesterM=09
    )
    IF %Month%==11 (
    SET YesterD=31
    SET YesterM=10
    )
    IF %Month%==12 (
    SET YesterD=30
    SET YesterM=11
    )
    GOTO:EOF

    :LeapYear

    SET /A mod400 = %Year% %% 400
    SET /A mod100 = %Year% %% 100
    SET /A mod4 = %Year% %% 4
    IF %mod400% EQU 0 (
    SET YesterD=29
    ) ELSE (
    IF %mod100% EQU 0 (
    SET YesterD=28
    ) ELSE (
    IF %mod4% EQU 0 (
    SET YesterD=29
    ) ELSE (
    SET YesterD=28
    )
    )
    )
    GOTO:EOF
    9