• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Date issue

Advisor ,
Mar 25, 2014 Mar 25, 2014

Copy link to clipboard

Copied

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!

Views

634

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Mar 28, 2014 Mar 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,

Votes

Translate

Translate
LEGEND ,
Mar 25, 2014 Mar 25, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Mar 25, 2014 Mar 25, 2014

Copy link to clipboard

Copied

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

or Any other idea?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 26, 2014 Mar 26, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 26, 2014 Mar 26, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Mar 28, 2014 Mar 28, 2014

Copy link to clipboard

Copied

LATEST

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,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation