JSI Tip 10210. Another way to return a 4 digit year, 2 digit month, and 2 digit day from a short date, regardless of your short date format.
February 28, 2006
NOTE: See tip 10272 Another way to return a 4 digit year, 2 digit month, and 2 digit day from a short date, regardless of your short date format.
In How can I return a 4 digit year, 2 digit month, and 2 digit day from a short date, regardless of your short date format? we returned separate variables for Year, Month, and Day.
I have scripted sDateSetup.bat to generate sDateCFG.bat, sDate.bat, sDate0.bat, sDate1.bat, and sDat2.bat to make the process faster.
NOTE: sDateSetup.bat generates the batch scripts in the same folder that sDateSetup.bat is stored in, which should be in your PATH.
To generate the scripts, run sDateSetup.bat without any switches.
To return a 4 digit year, 2 digit month, and 2 digit day from a short date:
call sDate sDT YYYY MM DD
Where:
sDT is the short date that you wish to enumerate.YYYY is a <a href="https://www.itprotoday.com/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> that will contain sDate's 4 digit year.MM is a <a href="https://www.itprotoday.com/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> that will contain sDate's 2 digit month.DD is a <a href="https://www.itprotoday.com/article/jsifaq/jsi-tip-5535-how-can-i-cause-a-called-batch-file-to-return-a-call-directed-environment-variable-.aspx">call directed environment variable</a> that will contain sDate's 2 digit day.
NOTE: These scripts require REG.EXE, built into Windows XP, Windows Server 2003, and later operating systems, or installed from the Windows 2000 Support Tools.
NOTE: If you change your short date format, you must re-run sDateSetup.bat.
NOTE: sDateSetup.bat is faster than iDateYMD.bat because the short date format is only retrieved from the registry once.
sDateSetup.bat contains:
@echo offsetlocalset Type=Nset sChar=#set fold=%~DP0#set fold=%fold:#=%set fold=%fold:#=%for /f "Tokens=2,3" %%x in ('REG QUERY "HKCUControl PanelInternational" /V iDate^|find /i "REG_SZ"') do set Type=%%yfor /f "Tokens=2,3" %%x in ('REG QUERY "HKCUControl PanelInternational" /V sDate^|find /i "REG_SZ"') do set sChar=%%yif "%Type%" EQU "3" goto err1if "%sChar%" EQU "#" goto err2if "%Type%" EQU "0" goto OKif "%Type%" EQU "1" goto OKif "%Type%" EQU "2" goto OK@echo sDateSetup Error - iDate=%Type%set Type=3endlocalgoto :EOF:err1@echo sDateSetup Error - iDate not found.set Type=3endlocalgoto :EOF:err2@echo sDateSetup Error - sDate not found.set Type=3endlocalgoto :EOF:OKset Type=Y@echo.@echo off>"%fold%sDateCFG.bat"@echo.for /f "Tokens=2,3" %%%%x in ('REG QUERY "HKCUControl PanelInternational" /V iDate^^^|find /i "REG_SZ"') do set $$sDate$Type=%%%%y>>"%fold%sDateCFG.bat"@echo.@echo off>"%fold%sDate.bat"@echo.if {%%4}
{} @echo Syntax: sDate sDT YYYY MM DD^&goto :EOF>>"%fold%sDate.bat"@echo.if {%%$$sDate$Type%%}
{} call sDateCFG>>"%fold%sDate.bat"@echo.if {%%$$sDate$Type%%}
{} @echo Syntax: sDate %%1 %%2 %%3 %%4 - sDateCFG failed^&goto :EOF>>"%fold%sDate.bat"@echo.call sdate%%$$sDate$Type%% %%1 %%2 %%3 %%4>>"%fold%sDate.bat":MDY@echo.@echo off>"%fold%sDate0.bat"@echo.if {%%4}
{} @echo Syntax: sDate0 sDT YYYY MM DD^&goto :EOF>>"%fold%sDate0.bat"@echo.setlocal>>"%fold%sDate0.bat"@echo.for /f "Tokens=1-3 Delims=%sChar%" %%%%a in ('@echo %%1') do (>>"%fold%sDate0.bat"@echo. set /a YY=10000%%%%c%%%%10000>>"%fold%sDate0.bat"@echo. set /a MM=100%%%%a%%%%100>>"%fold%sDate0.bat"@echo. set /a DD=100%%%%b%%%%100>>"%fold%sDate0.bat"@echo.^)>>"%fold%sDate0.bat"@echo.If %%YY%% LSS 10 set /a YY=200%%YY%%>>"%fold%sDate0.bat"@echo.If %%YY%% LSS 80 set YY=20%%YY%%>>"%fold%sDate0.bat"@echo.If %%YY%% LSS 100 set YY=19%%YY%%>>"%fold%sDate0.bat"@echo.If %%MM%% LSS 10 set MM=0%%MM%%>>"%fold%sDate0.bat"@echo.If %%DD%% LSS 10 set DD=0%%DD%%>>"%fold%sDate0.bat"@echo.endlocal^&set %%2=%%YY%%^&set %%3=%%MM%%^&set %%4=%%DD%%>>"%fold%sDate0.bat":DMY@echo.@echo off>"%fold%sDate1.bat"@echo.if {%%4}
{} @echo Syntax: sDate1 sDT YYYY MM DD^&goto :EOF>>"%fold%sDate1.bat"@echo.setlocal>>"%fold%sDate1.bat"@echo.for /f "Tokens=1-3 Delims=%sChar%" %%%%a in ('@echo %%1') do (>>"%fold%sDate1.bat"@echo. set /a YY=10000%%%%c%%%%10000>>"%fold%sDate1.bat"@echo. set /a MM=100%%%%b%%%%100>>"%fold%sDate1.bat"@echo. set /a DD=100%%%%a%%%%100>>"%fold%sDate1.bat"@echo.^)>>"%fold%sDate1.bat"@echo.If %%YY%% LSS 10 set /a YY=200%%YY%%>>"%fold%sDate1.bat"@echo.If %%YY%% LSS 80 set YY=20%%YY%%>>"%fold%sDate1.bat"@echo.If %%YY%% LSS 100 set YY=19%%YY%%>>"%fold%sDate1.bat"@echo.If %%MM%% LSS 10 set MM=0%%MM%%>>"%fold%sDate1.bat"@echo.If %%DD%% LSS 10 set DD=0%%dd%%>>"%fold%sDate1.bat"@echo.endlocal^&set %%2=%%YY%%^&set %%3=%%MM%%^&set %%4=%%DD%%>>"%fold%sDate1.bat":YMD@echo.@echo off>"%fold%sDate2.bat"@echo.if {%%4}
{} @echo Syntax: sDate2 sDT YYYY MM DD^&goto :EOF>>"%fold%sDate2.bat"@echo.setlocal>>"%fold%sDate2.bat"@echo.for /f "Tokens=1-3 Delims=%sChar%" %%%%a in ('@echo %%1') do (>>"%fold%sDate2.bat"@echo. set /a YY=10000%%%%a%%%%10000>>"%fold%sDate2.bat"@echo. set /a MM=100%%%%b%%%%100>>"%fold%sDate2.bat"@echo. set /a DD=100%%%%c%%%%100>>"%fold%sDate2.bat"@echo.^)>>"%fold%sDate2.bat"@echo.If %%YY%% LSS 10 set /a YY=200%%YY%%>>"%fold%sDate2.bat"@echo.If %%YY%% LSS 80 set YY=20%%YY%%>>"%fold%sDate2.bat"@echo.If %%YY%% LSS 100 set YY=19%%YY%%>>"%fold%sDate2.bat"@echo.If %%MM%% LSS 10 set MM=0%%MM%%>>"%fold%sDate2.bat"@echo.If %%DD%% LSS 10 set DD=0%%DD%%>>"%fold%sDate2.bat"@echo.endlocal^&set %%2=%%YY%%^&set %%3=%%MM%%^&set %%4=%%DD%%>>"%fold%sDate2.bat"endlocal
About the Author
You May Also Like