Ask a Jedi: Issue with datefield and mask

This post is more than 2 years old.

Mathew asks:

i know you havent used cfform much but im attempting to use the datefield in 8, to display an inline datepicker etc. the datepicker works fine on first load, but then when I submit the , i get an error saying 1-Jul-2008 is an invalid date or time string.

Mathew is right. I haven't played much at all with some of the newer form controls, but let's take a quick look at what he is seeing. First, the code: <cfparam name="form.etic" default="#now()#">

<cfform action="#cgi.SCRIPT_NAME#" preservedata="yes" method="post"> <cfinput type="dateField" name="etic" mask="DD-MMM-YY" style="font-size:12px;text-align:center" width="75" > <input type="submit" name="btnSubmit" value="PRESS ME"> </cfform>

There isn't anything too special in there. When run, you see a simple form with the datefield. You see a form field with 02-Jul-08. It doesn't matter if you change the date. As soon as you hit submit you get:

02-Jul-08 is an invalid date or time string.

So what's going wrong here? As always, it helps if you examine the 'flow' of the script. We begin by defaulting form.etic to the current date and time. If you output that value you would see:

{ts '2008-07-02 08:37:22'}

We then create a form. In the cfinput/type=datefield control, we pass in the date/time and use a mask to format it.

When we submit the form, here is where things get wonky. I added a cfdump on form to help figure this out. On submitting the form, the value of the form.etic is now:

02-Jul-08

When ColdFusion tries to treat this as a date for the datefield, it barfs. Why? I don't know. This is especially odd when you consider the solution. I changed his cfparam to this:

<cfif not structKeyExists(form, "etic")> <cfset form.etic = now()> <cfelse> <cfset form.etic = parseDateTime(form.etic)> </cfif>

ColdFusion has no problems parsing the masked date. This returns a value that works just fine within the datefield. Something to watch out. I'd also assume that you would want to use this before saving the data - although I bet a cfqueryparam with the sql type set to date would handle it fine. (If not, we know what function to use!)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Steve Walker posted on 7/2/2008 at 7:32 PM

I have found that the mask doesn't work very well except in the Flash forms and even then it is tricky. I set the value of the date input DateFormat(Now(),"mm/dd/yyyy") and it works beautifully. My CFC knows it is a date.

Comment 2 by Andy Sandefer posted on 7/2/2008 at 8:36 PM

Be careful if you're doing and insert and the datefield on the form is optional. Handling of this can vary from one RDBMS to another. With a CF/MySQL setup you can handle this by checking the submitted form's datefield value and then controlling what to send to your SQL statement.

An example would be...

<cfif (FORM.myDateField NEQ "")>
<cfset datefieldVariable = DateFormat(FORM.myDateField,"MM/DD/YYYY")>
<cfelse>
<cfset datefieldVariable = "null">
</cfif>

Then in your SQL statement just insert the value of datefieldVariable. I can't remember if I had to do anything like this in SQL Server - I probably didn't and was just able to pass the blank string from the form.

Andy Sandefer

Comment 3 by Raymond Camden posted on 7/2/2008 at 8:39 PM

I'd probably do that all in the cfqueryparam:

<cfif form.mydatefield neq "">
<cfqueryparam cfsqltype="cf_sql_date" value="#dateformat(etc)#">
<cfelse>
<cfqueryparam cfsqltype="cf_sql_date" null="true">
</cfif>

Comment 4 by Andy Sandefer posted on 7/2/2008 at 8:39 PM

My bad, I should've just told you to use

<cfif>
<cfset datefieldVariable = CreateODBCDate(FORM.myDateField)>
<cfelse>
<cfset datefieldVariable = "null">
</cfif>

Comment 5 by Andy Sandefer posted on 7/2/2008 at 8:41 PM

Yeah - he's right - just use the queryparam

Comment 6 by Neil Grimes posted on 10/14/2009 at 12:30 PM

best way to get around this is to use something like CONVERT(nvarchar(10), MP.master_page_expire_date, 3) in your db query to only pull out the bits of the date you need. My problem was the db : when I queried it the date was being returned in this format 10/12/2009 12:00:00:000

Comment 7 by Jessica posted on 2/24/2010 at 11:38 PM

I'm not certain that this is the same problem, however, when the cfinput/datefield value is passed (thru a CFAJAXPROXY call) to a JavaScript function, the creation of a new date object fails. The cfinput datefield is masked as DD-MMM-YYYY so it's sending the month text equivalent to the script. Is the best way to use JavaScript date functions to rewrite the string value back into a numeric format? Is there an easier way to manage this using built-in CF functionality? Thanks...

Comment 8 by Raymond Camden posted on 2/27/2010 at 7:17 AM

Not sure - do you have an example set up I could run locally?

Comment 9 by Richard posted on 3/14/2010 at 3:59 AM

Hi Ray, we have been using date mask in form on CF7 like:
<cfinput type="text" name="test" validate="eurodate" mask="99/99/9999" .../>
Then we upgraded to CF9, and the mask stopped working! I have tried a few things but cannot see why it should not and could not see anything from the adobe site or anywhere else...do you have any clues?

Many thanks

Comment 10 by Raymond Camden posted on 3/15/2010 at 3:39 PM

This worked for me, CF9:

<cfform name="foo">
<cfinput type="text" name="test" validate="eurodate" mask="99/99/9999" validateat="onblur" />
<input type="submit">
</cfform>

Comment 11 by Richard posted on 3/16/2010 at 2:58 AM

not luck...must something in our app/server
thanks

Comment 12 by Gov posted on 5/19/2010 at 7:58 PM

Thanks, just ran into this issue '... is an invalid date or time string' using a cfinput type 'datefield'.