EasyCFM.COM ColdFusion Forums / Coding Help! / strange date error

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: strange date error

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

Redmanz
08-26-2003 @ 10:59 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 411
Joined: Oct 2002

Okay I insert the birthdate into the db. Now it pulls an error saying cannot convert to number! The strange part is the database has two other test entries with the same formatted type of date in them and they pull no errors! Checked the database online and everything is the same for all three test profiles....other than one pulls this error!

Heres the code that is now pulling the error!

<cfset theDate = dateFormat(now(),"YYYY")>
<cfset theBirth = dateFormat(memberProfile.birthdate , "YYYY")>
<cfset theAge = theDate - theBirth>


Coldfusion Applications designed for learning available:
http://www.ideaz.ca/rez_productions/active_projects/index.htm

Webmaster
08-26-2003 @ 11:13 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4533
Joined: Jan 2002

Why not just do this ( its faster too Smile  )

<cfset theAge = DateDiff("yyyy", memberProfile.birthdate, now()>

Thanks,
Pablo Varando

=====================================================

CJ
08-26-2003 @ 11:16 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4262
Joined: Oct 2002

couple of things:

1) instead of #dateFormat(myDateVal, 'yyyy')#, i'd suggest doing #year(myDateVal)#.  I think it's less likely to error out.

2) instead of using 3 lines of code to get the age, you can just use a dateDiff() function.  <cfset theAge = dateDiff('y', now(), memberProfile.birthdate)>

3) #2 above should negate the need for the code you're currently using...but out of curiousity, do a <cfoutput> on #theDate# and #theBirth#.  I'm curious to see what values are being returned Smile  



CJ
@ #coldfusion/DALNet
http://charlie.griefer.com

Webmaster
08-26-2003 @ 11:18 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4533
Joined: Jan 2002

beat ya Playful Wink

Thanks,
Pablo Varando

=====================================================

CJ
08-26-2003 @ 11:21 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4262
Joined: Oct 2002

<shaking fist>
     d*mn you webmaster!
</shaking fist>

Playful

CJ
@ #coldfusion/DALNet
http://charlie.griefer.com

Webmaster
08-26-2003 @ 11:29 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4533
Joined: Jan 2002

Playful Wink

Thanks,
Pablo Varando

=====================================================

Redmanz
08-26-2003 @ 11:37 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 411
Joined: Oct 2002

Wierd.......gotta look at the insert maybe the error is there, with the suggested changes everything works as it should for the first two test profiles! But for the last one still no banana! The same cfsets......etc........same field........but an error


the error:
Parameter 2 of function DateDiff which is now "" must be a date/time value


Coldfusion Applications designed for learning available:
http://www.ideaz.ca/rez_productions/active_projects/index.htm

CJ
08-26-2003 @ 11:48 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4262
Joined: Oct 2002

seems that you're not pulling a value from the db for the birthday for that record.

do a <cfdump> of your query and see what kind of recordset you're working with.



CJ
@ #coldfusion/DALNet
http://charlie.griefer.com

Redmanz
08-26-2003 @ 4:32 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Senior Member
Posts: 411
Joined: Oct 2002

Wierd I thought maybe it was something in the rest of the query so I stripped everything out.......and put together this simple sample file. Now when it was with the rest of the query remember the first two test results showed properly and the last one didn't! Now after stripping every thing out....nothing works....just gives the same error. Yup there is data in the field etc.........triple checked that and the field is set to date.........etc



<cfquery name="memberProfile" datasource="#dsn#">
SELECT           id,
          birthdate
          
WHERE id = 1
</cfquery>



<cfset theAge = DateDiff('yyyy',memberProfile.birthdate, now())>

<cfoutput>#theAge#</cfoutput>


Coldfusion Applications designed for learning available:
http://www.ideaz.ca/rez_productions/active_projects/index.htm

CJ
08-26-2003 @ 6:13 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Administrator
Posts: 4262
Joined: Oct 2002

<ahem> as i suggested earlier Smile  </ahem> ...

do a <cfdump> to see what you're pulling FROM the database.  doesn't matter if you know the field is a date/time datatype.  in theory, yes, that should mean everything is peachy.  we live in an imperfect world, however.

based on the code you have above, I would do three things in the name of debugging:

1) <cfdump var="#memberProfile#">
2) <cfoutput>#memberProfile.birthdate#</cfoutput>
3) <cfoutput>#isDate(memberProfile.birthdate)#</cfoutput>

post back with the results of those 3 things.



CJ
@ #coldfusion/DALNet
http://charlie.griefer.com

Sponsored By...
iOpenSoft, LLC is a Houston, Texas Advanced Technology Studio Specializing in Web Design, Web Development, iPhone App Development and Android App Development.