JSI Tip 8181. How can I insure that a year, month, and day contain 4,2, and 2 digits, respectively?

Jerold Schulman

June 22, 2004

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


I have scripted YYYYMMDD.bat to insure that the year, month, and day components of a date contain 4,2, and 2 digits, respectively.

The syntax for using YYYYMMDD.bat is:

call yyyymmdd YY MM DD

Where:

YY is the name of the variable that contains the year.
MM is the name of the variable that contains the month.
DD is the name of the variable that contains the day.

Sample Usage

If you used UnivDate.bat to return the YY, MM, and DD environment variables, then:

call yyyymmdd yy mm dd

would insure that the year is 4 digits, and that the month and day are 2 digits, regardless of the short date (sShortDate) format.

NOTE: If you open a CMD prompt and type date and then press Enter, you can see that the second line of theoutput does contain a model of the date format that UnivDate.bat uses to name the year, month, and day environment variables.

NOTE: If after calling YYYYMMDD.bat, you wanted the year environment variable to contain the last 2 digits of the 4 digit year, use a command similar to:

set YY=%YY:~2,2%

NOTE: See A faster way to insure that a year, month, and day contain 4,2, and 2 digits, respectively?

YYYYMMDD.bat contains:

@echo offif {%3}=={} @echo YYYYMMDD YY MM DD&goto :EOFsetlocalcall set YY=%%%1%%call set MM=%%%2%%call set DD=%%%3%%set /a YY=10000%YY%%%10000set /a MM=100%MM%%%100set /a DD=100%DD%%%100if %DD% LSS 10 set DD=0%DD%if %MM% LSS 10 set MM=0%MM%if %YY% LSS 10 set YY=200%YY%if %YY% LSS 100 set YY=20%YY%endlocal&set %1=%YY%&set %2=%MM%&set %3=%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