Search code examples
timecoldfusioncfml

Verifying if string is in a valid time format


I know how to verify if something is a valid date

mydate = "1/2/2001";
writeoutput(isDate(mydate));

How do I verify if it is a valid time?

mytime = "12:42 pm";
writeoutput(isTime(mytime));

Does not work because isTime() does not exist.


Solution

  • To find out if something is a valid time, the isDate() function works too.

    mytime = "12:42 pm";
    writeoutput(isDate(mytime));
    

    Update

    Based on feedback from BKBK, I looked for an approach that does not accept so many variations. I found this

    locale = getLocale();
    
    writeOutput("Locale is: " & locale & "<br>");
    
    // accepted
    writeoutput(LSisDate("12:42 pm", locale));
    writeoutput(LSisDate("12:42", locale));
    writeoutput(LSisDate("22:00", locale));
    writeoutput("<hr />");
    // rejected
    writeoutput(LSisDate("1a", locale));
    writeoutput(LSisDate("2p", locale));
    writeoutput(LSisDate("2:70", locale));
    writeoutput(LSisDate("42:00", locale));
    writeoutput("<hr />");
    // rejected
    writeoutput(LSisDate("12 42 pm", locale));
    writeoutput(LSisDate("12 42", locale));
    writeoutput(LSisDate("22 00", locale));