Skip to main content
Inspiring
March 25, 2014
Answered

Date issue

  • March 25, 2014
  • 2 replies
  • 809 views

Hi all,

I have an issue validating dates, my sample code:

<cfoutput>

<cfset value1 = "21A">

<cfset value2 = "05/02/11 12:45">

<cfset tempDate= "05/02/11 12:45">

<cfset value3 = "#DateConvert( 'Local2UTC', tempDate)#">

<cfset dateValue = value1 />

#dateValue #<br/>

IsNumericDate - #IsNumericDate(dateValue )#<br/>

isValid (USDATE) - #isValid("USDate",dateValue )#<br/>

isValid (DATE) - #isValid("Date",dateValue )#<br/>

isValid (USDATE, dateFormat) - #isValid("USDate",dateFormat(dateValue ,'mm/dd/yyyy'))#<br/>

<cfif isNumeric(dateValue)>

    number

<cfelseif IsNumericDate(dateValue) and isValid("Date", dateValue) >

    date

<cfelse>

    varchar

</cfif>

</cfoutput>

Why CF9 thinks that 21A is date?

If I use UTC date value isValid("USDate",dateValue ) = NO

How can I do better date validation?

Any ideas?

Thanks!

    This topic has been closed for replies.
    Correct answer jfb00

    Thanks for you reply and help!

    I may found a good RegEx that covers date format for "mm/dd/yy", "mm/dd/yyyy", "Aug 1, 2010", "September 3, 2010" and "DD-MMM-YY".

    <cfset dateRegExp = "(\d+/\d+/\d+)|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(t|tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s\d+(st|nd|rd|th)?,\s\d{4})|(\d+-\w{3}-\d+)" />

    http://www.raymondcamden.com/index.cfm/2011/8/20/Finding-dates-in-a-string-using-ColdFusion

    Best,

    2 replies

    BKBK
    Community Expert
    Community Expert
    March 26, 2014

    jfb00 wrote:

    Why CF9 thinks that 21A is date?

    I think it is because ColdFusion interpretes the string as a time value. ColdFusion is a weakly-typed language and simply makes an intelligent guess. The string starts with a whole number, followed by A, which may stand for A.M.. Times are dates. The default day of the year is 30th December 1899.

    If I use UTC date value isValid("USDate",dateValue ) = NO

    The correct answer is indeed no. IsValid("USDate",dateValue ) tests for a date of the form 'mm/dd/yy' or 'mm/dd/yyyy'.

    How can I do better date validation?

    There is no universal format for dates. So you should first specify the full datetime format you want, then validate against that format.

    For example, suppose you want dates to be of the form "dd/mm/yyyy HH:MM:SS". Then you should begin by parsing the input to fish out the values of dd, mm, yyyy, HH, MM, and SS. Use these as arguments in createdatetime(). Then check the resulting datetime with isDate() or isValid().

    jfb00AuthorCorrect answer
    Inspiring
    March 28, 2014

    Thanks for you reply and help!

    I may found a good RegEx that covers date format for "mm/dd/yy", "mm/dd/yyyy", "Aug 1, 2010", "September 3, 2010" and "DD-MMM-YY".

    <cfset dateRegExp = "(\d+/\d+/\d+)|((Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(t|tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s\d+(st|nd|rd|th)?,\s\d{4})|(\d+-\w{3}-\d+)" />

    http://www.raymondcamden.com/index.cfm/2011/8/20/Finding-dates-in-a-string-using-ColdFusion

    Best,

    WolfShade
    Legend
    March 25, 2014

    RegEx.  Choose one (or two) date formats that will be considered 'valid', create masks for them, and test against those.

    jfb00Author
    Inspiring
    March 25, 2014

    My data can have multiple date formats including UTC. It will be a complex RegEx, any expert in RegEx?

    or Any other idea?

    WolfShade
    Legend
    March 26, 2014

    Hate to split hairs, but UTC isn't a date format, it's a time zone.

    Formats would be YYYY-MM-DD vs MM-DD-YYYY vs DD-MM-YYYY, or alternatively using slashes instead of dashes.  And then do you also want the time included with that?  If so, 12h vs 24h, etc.