JSI Tip 10480. How can I validate that a string is a valid date or time, or date and time?


I have scripted DateOK.bat to determine if a parameter contains a valid date or time, or date and time.

The syntax for using DateOK.bat is:

\[Call\] DateOK String TrueFalse

Where:

String        is the date or time or date/time that you want to validate.

TrueFalse is a call directed environment variable that will contain a T if the string can be converted to a valid date or time or date/time, or a F if the string is not valid.

DateOK.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: DateOK DateTime TF&goto :EOF
if exist "%TEMP%\DateOK.vbs" goto doit
@echo if isDate(Wscript.Arguments(0)) Then>"%TEMP%\DateOK.vbs"
@echo.     Wscript.Echo "T">>"%TEMP%\DateOK.vbs"
@echo Else>>"%TEMP%\DateOK.vbs"
@echo.     Wscript.Echo "F">>"%TEMP%\DateOK.vbs"
@echo End If>>"%TEMP%\DateOK.vbs"
:doit
for /f "Tokens=*" %%d in ('cscript //nologo "%TEMP%\DateOK.vbs" %1') do (
 set %2=%%d
)



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