There is also a not short but simple batch only code that may resolve it:
Code: Select all
@echo off
cls
setlocal
call ::initDateFormats
call :parseDateStringToMMDDYYYY MM DD YYYY
echo SET_DATE_FORMAT = "%SET_DATE_FORMAT%"
echo DATE_FORMAT = "%DATE_FORMAT%"
echo %MM%/%DD%/%YYYY%
endlocal
goto:eof
:initDateFormats
::
setlocal enableDelayedExpansion
:: save current date
set "CURRENT_DATE=%DATE%"
:: build SET_DATE_FORMAT losing actual pc date
(set "NOT=" & for /f "tokens=*" %%a in ('date 90-90-90') do set "NOT=%%a" )<nul
(set "YY_1=" & for /f "tokens=*" %%a in ('date 90-01-01') do set "YY_1=%%a")<nul
(set "YY_2=" & for /f "tokens=*" %%a in ('date 01-90-01') do set "YY_2=%%a")<nul
(set "DD_1=" & for /f "tokens=*" %%a in ('date 20-01-01') do set "DD_1=%%a")<nul
(set "DD_2=" & for /f "tokens=*" %%a in ('date 01-20-01') do set "DD_2=%%a")<nul
if not "%YY_1%" == "%NOT%" (if "%DD_2%" == "%NOT%" ( set "SET_DATE_FORMAT=YY-MM-DD" ) else ( set "SET_DATE_FORMAT=YY-DD-MM" )
) else if not "%YY_2%" == "%NOT%" (if "%DD_1%" == "%NOT%" ( set "SET_DATE_FORMAT=MM-YY-DD" ) else ( set "SET_DATE_FORMAT=DD-YY-MM" )
) else (if "%DD_1%" == "%NOT%" ( set "SET_DATE_FORMAT=MM-DD-YY" ) else ( set "SET_DATE_FORMAT=DD-MM-YY" )
)
:: build DATE_FORMAT losing actual date
call ::setDateMMDDYY 02 01 03
call :dateTokenize TOKEN DATE
set "DATE_FORMAT=%DATE%" & for %%a in (%TOKEN%) do (
if "%%a" == "01" ( set "DATE_FORMAT=!DATE_FORMAT:01=DD!"
) else if "%%a" == "1" ( set "DATE_FORMAT=!DATE_FORMAT:1=D!"
) else if "%%a" == "02" ( set "DATE_FORMAT=!DATE_FORMAT:02=MM!"
) else if "%%a" == "2" ( set "DATE_FORMAT=!DATE_FORMAT:2=M!"
) else if "%%a" == "2003" ( set "DATE_FORMAT=!DATE_FORMAT:2003=YYYY!"
) else if "%%a" == "03" ( set "DATE_FORMAT=!DATE_FORMAT:03=YY!"
) else if "%%a" == "3" ( set "DATE_FORMAT=!DATE_FORMAT:3=Y!"
) else ( set "DATE_FORMAT=!DATE_FORMAT:%%a=DDDD!"
)
set /a "INDEX+=1"
)
:: restore current date
call :setDateString CURRENT_DATE
:: save DATE_FORMAT and SET_DATE_FORMAT
endlocal & set "DATE_FORMAT=%DATE_FORMAT%" & set "SET_DATE_FORMAT=%SET_DATE_FORMAT%"
goto:eof
:dateTokenize
:: %1 variable returning the tokens
:: %2 variable containing date string representation
setlocal enableDelayedExpansion
set "TOKEN=!%2!"
set "TOKEN=%TOKEN:,= %"
set "TOKEN=%TOKEN:-= %"
set "TOKEN=%TOKEN:.= %"
set "TOKEN=%TOKEN:/= %"
set "TOKEN=%TOKEN::= %"
set "TOKEN=%TOKEN:;= %"
set "TOKEN=%TOKEN:\= %"
endlocal & set "%1=%TOKEN%"
goto :eof
:setDateMMDDYY
:: SET_DATE_FORMAT must exist and be valid
:: %1 day to set
:: %2 month to set
:: %3 year to set
setlocal enableDelayedExpansion
set "DATE_STRING=%SET_DATE_FORMAT%"
set "DATE_STRING=!DATE_STRING:MM=%1!"
set "DATE_STRING=!DATE_STRING:DD=%2!"
set "DATE_STRING=!DATE_STRING:YY=%3!"
date %DATE_STRING%
endlocal
goto :eof
:setDateString
:: DATE_FORMAT and SET_DATE_FORMAT must exist and be valid
:: %1 variable storing the date string to set
setlocal enableDelayedExpansion
call :parseDateStringToMMDDYYYY MM DD YYYY %1
call :setDateMMDDYY %MM% %DD% %YYYY:~2,2%
endlocal
goto :eof
:parseDateStringToMMDDYYYY
:: DATE_FORMAT and SET_DATE_FORMAT must exist and be valid
:: %1 variable name for returning the month
:: %2 variable name for returning the day
:: %3 variable name for returning the year
:: %4 variable name containing the Date
setlocal enableDelayedExpansion
set "DATE_STRING=%4"
if not defined DATE_STRING set "DATE_STRING=DATE"
:: storing the indices of day, month and year in D, M, and Y
call :dateTokenize TOKEN DATE_FORMAT
set "Y=" & set "M=" & set "D=" & set "INDEX=0" & for %%a in (%TOKEN%) do (
if "%%a" == "DD" ( set "D=!INDEX!"
) else if "%%a" == "D" ( set "D=!INDEX!"
) else if "%%a" == "MM" ( set "M=!INDEX!"
) else if "%%a" == "M" ( set "M=!INDEX!"
) else if "%%a" == "YYYY" ( set "Y=!INDEX!"
) else if "%%a" == "YY" ( set "Y=!INDEX!"
) else if "%%a" == "Y" ( set "Y=!INDEX!"
)
set /a "INDEX+=1"
)
:: storing the values of day, month and year in D, M, and Y
call :dateTokenize TOKEN %DATE_STRING%
set "INDEX=0" & for %%a in (%TOKEN%) do (
if "!INDEX!" == "%D%" ( SET "D=%%a"
) else if "!INDEX!" == "%M%" ( SET "M=%%a"
) else if "!INDEX!" == "%Y%" ( SET "Y=%%a"
)
set /a "INDEX+=1"
)
if "%D:~1,1%" == "" set "D=0%D%"
if "%M:~1,1%" == "" set "M=0%M%"
if "%Y:~1,1%" == "" set "Y=200%Y%"
:: 1930 to 2029 is the default setting, maybe changed: then you have to change this, too.
if %Y% LSS 30 set "Y=20%Y%"
if %Y% LSS 100 set "Y=19%Y%"
endlocal & set "%1=%M%" & set "%2=%D%" & set "%3=%Y%"
goto:eof
This was the solution (call ::initDateFormats) that should be used only on autoexec.bat within dos, so you can use the functions below.
When running Windows it is not recommended as even on boot time setting the date to multiple values can have various side effects.
For this you can set the two format strings manually for a user, so you can make use of the other (not resetting dates) functions.
penpen
Edit: If no side effects are assumed this code also works on windows cmd.exe, and you can copy the date format strings from the shell.
Edit2:You should take care of side effects (
harmless word, but critical results):
For example if a virus scanner runs only for 4 years, or so, and you run this
batch it may be noticed by the virus scanner and confusing it, as
this batch sets multiple dates, and after that restoring the current date.
This result may be, that the virus scanner believes a couple of years have passed, and the 4 year period just ended.
Because of such things is it not recommended, to run this initializiation under windows 95+ (only recommended for MS-DOS 6.xx, 7.0x, 71x)