5 Replies Latest reply on Mar 28, 2014 4:00 AM by jfb00

    Date issue

    jfb00 Level 3

      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!

        • 1. Re: Date issue
          WolfShade Level 4

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

          1 person found this helpful
          • 2. Re: Date issue
            jfb00 Level 3

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

            or Any other idea?

            • 3. Re: Date issue
              WolfShade Level 4

              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.

              • 4. Re: Date issue
                BKBK Adobe Community Professional & MVP

                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().

                1 person found this helpful
                • 5. Re: Date issue
                  jfb00 Level 3

                  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)?|S ep(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-ColdFusio n

                   

                  Best,