quinekxi
quinekxi

Reputation: 889

How to format date based on the windows locale using batch?

I have a batch file that will compare dates. The problem is we need to test it in different setup, different language means different date format.

e.g Get the modified date of the Test.log file (not knowing what the locale date format is)

for %%i in (c:\Test.log) do (
call :testprocess %%~ti
)

I want to format the modified date of Test.log in yyyy/MM/dd.

How could I achieve it without knowing what is the format of locale date?

Upvotes: 1

Views: 1886

Answers (1)

quinekxi
quinekxi

Reputation: 889

I've found a solution on this. I lost the link of my reference. So I tweak some code and use this on my needs.

    for %%i in (c:\Test.log) do (
    call :testprocess %%~ti
    )

    :testprocess
    set temptime=%1

Search the key International in the registry the get the value of iDate and sDate.

    set KEY_DATE="HKCU\Control Panel\International"
    FOR /F "usebackq skip=4 tokens=1,3" %%A IN (`REG QUERY %KEY_DATE% /v iDate 2^>nul`) DO set iDate=%%B
    FOR /F "usebackq skip=4 tokens=1,3" %%A IN (`REG QUERY %KEY_DATE% /v sDate 2^>nul`) DO set sDate=%%B

I used the sDate value as the delims= and used the iDate as of what the format of the locale date.

    FOR /F "tokens=1-3 delims=%sDate%" %%A IN ("%temptime%") DO (
        IF "%iDate%"=="0" (
            SET fdd=%%B
            SET fmm=%%A
            SET fyyyy=%%C
        )
        IF "%iDate%"=="1" (
            SET fdd=%%A
            SET fmm=%%B
            SET fyyyy=%%C
        )
        IF "%iDate%"=="2" (
            SET fdd=%%C
            SET fmm=%%B
            SET fyyyy=%%A
        )
    )

    echo %fyyyy%/%fMM%/%fdd%

You can also refer the ff. usage in here.

Hope this help to you guys.


To understand what are the meanings of the different values, check this link.

Upvotes: 2

Related Questions