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:

<b>sDT</b>   is the short date that you wish to enumerate.
<b>YYYY</b>  is a <a href="/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 <b>sDate</b>'s 4 digit year.
<b>MM</b>    is a <a href="/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 <b>sDate</b>'s 2 digit month.
<b>DD</b>    is a <a href="/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 <b>sDate</b>'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:

<font size="1">
@echo off
setlocal
set Type=N
set sChar=#
set fold=%~DP0#
set fold=%fold:\#=%
set fold=%fold:#=%
for /f "Tokens=2,3" %%x in ('REG QUERY "HKCU\Control Panel\International" /V iDate^|find /i "REG_SZ"') do set Type=%%y
for /f "Tokens=2,3" %%x in ('REG QUERY "HKCU\Control Panel\International" /V sDate^|find /i "REG_SZ"') do set sChar=%%y
if "%Type%" EQU "3" goto err1
if "%sChar%" EQU "#" goto err2
if "%Type%" EQU "0" goto OK
if "%Type%" EQU "1" goto OK
if "%Type%" EQU "2" goto OK
@echo sDateSetup Error - iDate=%Type%
set Type=3
endlocal
goto :EOF
:err1
@echo sDateSetup Error - iDate not found.
set Type=3
endlocal
goto :EOF
:err2
@echo sDateSetup Error - sDate not found.
set Type=3
endlocal
goto :EOF
:OK
set Type=Y
@echo.@echo off>"%fold%\sDateCFG.bat"
@echo.for /f "Tokens=2,3" %%%%x in ('REG QUERY "HKCU\Control Panel\International" /V iDate^^^|find /i "REG_SZ"') do set $$sDate$Type=%%%%y>>"%fold%\sDateCFG.bat"
@echo.@echo off>"%fold%\sDate.bat"
@echo.if \{%%4\}<h1><a name="_echo_Syntax_sDate_sDT_YYYY_MM_DD_amp_goto_EOF_gt_gt_fold_sDate_bat_echo_if_sDate_Type_">\{\} @echo Syntax: sDate sDT YYYY MM DD^&goto :EOF>>"%fold%\sDate.bat"
@echo.if \{%%$$sDate$Type%%\}</a></h1>\{\} call sDateCFG>>"%fold%\sDate.bat"
@echo.if \{%%$$sDate$Type%%\}<h1><a name="_echo_Syntax_sDate_1_2_3_4_sDateCFG_failed_amp_goto_EOF_gt_gt_fold_sDate_bat_echo_call_sdate_sDate_Type_1_2_3_4_gt_gt_fold_sDate_bat_MDY_echo_echo_off_gt_fold_sDate0_bat_echo_if_4_">\{\} @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\}</a></h1>\{\} @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\}<h1><a name="_echo_Syntax_sDate1_sDT_YYYY_MM_DD_amp_goto_EOF_gt_gt_fold_sDate1_bat_echo_setlocal_gt_gt_fold_sDate1_bat_echo_for_f_Tokens_1_3_Delims_sChar_a_in_echo_1_do_gt_gt_fold_sDate1_bat_echo_set_a_YY_10000_c_10000_gt_gt_fold_sDate1_bat_echo_set_a_MM_100_b_100_gt_gt_fold_sDate1_bat_echo_set_a_DD_100_a_100_gt_gt_fold_sDate1_bat_echo_gt_gt_fold_sDate1_bat_echo_If_YY_LSS_10_set_a_YY_200_YY_gt_gt_fold_sDate1_bat_echo_If_YY_LSS_80_set_YY_20_YY_gt_gt_fold_sDate1_bat_echo_If_YY_LSS_100_set_YY_19_YY_gt_gt_fold_sDate1_bat_echo_If_MM_LSS_10_set_MM_0_MM_gt_gt_fold_sDate1_bat_echo_If_DD_LSS_10_set_DD_0_dd_gt_gt_fold_sDate1_bat_echo_endlocal_amp_set_2_YY_amp_set_3_MM_amp_set_4_DD_gt_gt_fold_sDate1_bat_YMD_echo_echo_off_gt_fold_sDate2_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\}</a></h1>\{\} @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
</font>