JSI Tip 9885. 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?

Jerold Schulman

November 9, 2005

1 Min Read
ITPro Today logo in a gray background | ITPro Today


NOTE: See 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..

I have scripted iDateYMD.bat to return a short date's 4 digit year, 2 digit month, and 2 digit day, regardless of your short date format.

iDateYMD.bat retrieves your date format from the iDate Value Name, using REG.EXE, built into Windows XP and Windows Server 2003, or REG.EXE from the Windows 2000 Support Tools on the CD-ROM, which must be available in your PATH.

The syntax for using iDateYMD.bat is:

[call] iDateYMD sDate YYYY MM DD

Where:

sDate is the short date that you wish to enumerate.YYYY  is a call directed environment variable that will contain sDate's 4 digit year.MM    is a call directed environment variable that will contain sDate's 2 digit month.DD    is a call directed environment variable that will contain sDate's 2 digit day.

iDateYMD.bat contains:

@echo offif {%4}=={} @echo Syntax: iDateYMD sDate YYYY MM DD&goto :EOFsetlocalfor /f "tokens=1-3 delims=.:/-, " %%x in ('@echo %1') do ( set /a p1=10000%%x%%10000 set /a p2=10000%%y%%10000 set /a p3=10000%%z%%10000)if %p1% LSS 10 set p1=0%p1%if %p2% LSS 10 set p2=0%p2%if %p3% LSS 10 set p3=0%p3%set df=3for /f "Tokens=2,3" %%x in ('REG QUERY "HKCUControl PanelInternational" /V iDate^|find /i "REG_SZ"') do set df=%%yif "%df%" EQU "0" set MM=%p1%&set DD=%p2%&set YY=%p3%&goto daynif "%df%" EQU "1" set DD=%p1%&set MM=%p2%&set YY=%p3%&goto daynif "%df%" EQU "2" set YY=%p1%&set MM=%p2%&set DD=%p3%&goto daynset YY=0000&set MM=00&set DD=00:daynendlocal&set %2=%YY%&set %3=%MM%&set %4=%DD%



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like