Skip navigation

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


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 the output 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 off
if \{%3\}==\{\} @echo YYYYMMDD YY MM DD&goto :EOF
setlocal
call set YY=%%%1%%
call set MM=%%%2%%
call set DD=%%%3%%
set /a YY=10000%YY%%%10000
set /a MM=100%MM%%%100
set /a DD=100%DD%%%100
if %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%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish