6 Replies Latest reply on Mar 27, 2007 3:28 AM by kim il sung

    convert to a date error

    JohnGree Level 1
      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>
        • 1. Re: convert to a date error
          kim il sung
          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
          • 2. Re: convert to a date error
            JohnGree Level 1
            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>">
            • 3. Re: convert to a date error
              kim il sung Level 1
              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
              • 4. Re: convert to a date error
                Level 7
                > <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
                • 5. Re: convert to a date error
                  JohnGree Level 1
                  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>
                  • 6. Re: convert to a date error
                    kim il sung Level 1
                    did you read my previous message? answer is there.
                    cheers
                    kim