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

convert to a date error

Guest
Mar 26, 2007 Mar 26, 2007

Copy link to clipboard

Copied

i have a the code below which i am passing a form value to

<CFLOCATION url="CalendarCurrent.cfm?DATE=<cfoutput>#form.Diary_Date#</cfoutput>">

but i am getting cannot convert to a date error, i cannot change the code below as other pages use it, so how can i change the cflocation date to match.

i have tried dateformat and lsdateformat any ideas?

<cfif NOT IsDefined("URL.DATE")>
<cfset session.DATE= DateFormat(Now(), "dddd DD MMMM yyyy")>
<cfset session.SHORTDATE= DateFormat(Now(), "yyyy-mm-dd")>
<cfelse>
<cfset session.DATE= (DateFormat(URL.DATE, "dddd DD MMMM yyyy")) >
<cfset session.SHORTDATE= (DateFormat(URL.DATE, "yyyy-mm-dd")) >
</cfif>
TOPICS
Advanced techniques

Views

380

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
New Here ,
Mar 26, 2007 Mar 26, 2007

Copy link to clipboard

Copied

i guess you get error at conversion because you try to convert string to date object?
dateformat function just formats date objects
lsdateformat function checks given object is valid date object
try following code

cheers
Kim

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
Guest
Mar 26, 2007 Mar 26, 2007

Copy link to clipboard

Copied

ok i tried that but still get thid error
The value "27/3/2007" could not be converted to a date.

<!--- init myDate --->
<cfset myDate="form.Diary_Date">
<!--- parse myDate string to day, month and year strings --->
<cfset myDate_Month = "#GetToken(myDate,2,"\")#">
<cfset myDate_Day = "#GetToken(myDate,1,"\")#">
<cfset myDate_Year = "#GetToken(myDate,3,"\")#">

<cftry>
<!--- create date object from day, month and year strings, on error some of strings was not valid --->
<cfset myNewDate="#CreateDate( myDate_Month, myDate_Day, myDate_Year)#">
<cfcatch>
<cfoutput>error while creating date!</cfoutput>
</cfcatch>
</cftry>

<CFLOCATION url="CalendarCurrent.cfm?DATE=<cfoutput>#form.Diary_Date#</cfoutput>">

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
New Here ,
Mar 26, 2007 Mar 26, 2007

Copy link to clipboard

Copied

Hi,
You didnt understand some basic stuff, so i tell you what you should have done:
"The value "27/3/2007" could not be converted to a date."

<cfset myDate="form.Diary_Date">
this line you create myDate parameter with value from form, Diary_Date field.
you should use # signs because coldfusion doesnt know otherwise you want to use cf parameters value.
in other words otherwise you are telling coldfusion that mydate is string containing literaly "form..Diary_Date".

take care you always trim parameter values from forms, same browsers are so nice to adding extra enter character at end of field value. so first line goes:
<cfset myDate="#Trim(form.Diary_Date)#">

now, sence myDate parameter is fine, lets focus few minutes how you split value from myDate to tree different pieces.
GetToken fuction is nice fellow when you want split preformated string to pieces, preformated mean that string always look same kind. just like dates. year, month and day are in same positions at string, while year might be different.
example: 1.1.2007 and 1.1.2003, get it?
usage of gettoken is very simple, you just tell function first parameter where he has to split you piece of string and second
you tell number of piece to look for and last you tell what character is separator of each piece.
so, since date you want to process is "27/3/2007", date is first, month next and year last.
separator is "/" so, getting month should be like this:
myDate is parameter is strinng to be splitted.
number 2 is order number, month is second piece at string
/ is piece separator.
Following code will give you substring "3" from string "27/3/2007".
<cfset myDate_Month = "#GetToken(myDate,2,"/")#">

Can you repair your self next two lines?
<cfset myDate_Day = "#GetToken(myDate,1,"\")#">
<cfset myDate_Year = "#GetToken(myDate,3,"\")#">

Cheers
Kim

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
Guest
Mar 26, 2007 Mar 26, 2007

Copy link to clipboard

Copied

ok i tried that but now i get the error error while creating date!

how can i change this code below to work?

thanks

<!--- init myDate --->
<cfset myDate="#Trim(form.Diary_Date)#">
<!--- parse myDate string to day, month and year strings --->
<cfset myDate_Day = "#GetToken(myDate,1,"\")#">
<cfset myDate_Month = "#GetToken(myDate,2,"\")#">
<cfset myDate_Year = "#GetToken(myDate,3,"\")#">

<cftry>
<!--- create date object from day, month and year strings, on error some of strings was not valid --->
<cfset myNewDate="#CreateDate(myDate_Month, myDate_Day, myDate_Year)#">
<cfcatch>
<cfoutput>error while creating date!</cfoutput>
</cfcatch>
</cftry>

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
New Here ,
Mar 27, 2007 Mar 27, 2007

Copy link to clipboard

Copied

LATEST
did you read my previous message? answer is there.
cheers
kim

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, 2007 Mar 26, 2007

Copy link to clipboard

Copied

> <CFLOCATION
> url="CalendarCurrent.cfm?DATE=<cfoutput>#form.Diary_Date#</cfoutput>">

It doesn't matter what format your variables is in: you can't embed one CF
tag within another!

And... you don't have to, anyhow. Get rid of the <cfoutput> tags.

--
Adam

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