11 Replies Latest reply on Jul 28, 2008 10:39 PM by musicmp3

    date problem

    Nick201 Level 1
      i have date like 02/02/02008
      and i am using isdate function and its return true. any idea why?
      Year has 02008 which is incorrect. so how can i validate that.

      thanks
        • 1. Re: date problem
          mr. modus
          Well, date functions in CF seem to ignore leading zero's. So 02008 is equal to 2008. Therefore it is a date.

          What exactly are you trying to acheive here? If you want to disallow the value because it's more than four chars you could simply do this:

          <cfif Len(ListLast("02/02/02008","/")) GT 4>throw error</cfif>

          I wonder why it matters though, since you can work with 02008 the same as you can with 2008.

          Perhaps you could give more detail about why you are trying to invalidate the data.
          • 2. Re: date problem
            Nick201 Level 1
            because when i insert into db then it throw error because of 02008

            how can handle @ db insert level.
            • 3. Re: date problem
              mr. modus Level 1
              I'm guessing you aren't turning the date into an ODBC format?

              Try this:

              <cfset myDate = CreateODBCDate("02/02/02008")>
              • 4. Re: date problem
                mr. modus Level 1
                Nick201, did that work for you?
                • 5. Re: date problem
                  BKBK Adobe Community Professional & MVP
                  isDate() has a broad interpretation. I suppose it's because it takes a string as argument, whereas there are countless ways to represent a valid Coldfusion date as a string. For example, the following will produce a 'yes': <cfoutput>#isDate('1a')#</cfoutput>

                  If you want to store dates to the database, you could follow Mr. Modus' advice and apply createODBCDate().
                  • 6. Re: date problem
                    Dave DuPlantis
                    That's right, IsDate() is usually not the function you want to use in cases like this.

                    IsDate() will return true for anything that can be converted to a date value. It's not bad, as long as you don't care how the date is converted ... because many, many values can be converted to dates. (For example, just about any string that consists of numbers and separators.)

                    In most cases, you would want to perform some validation on the value so that you can have more confidence that the date parts are provided as you expect. This helps with day/month order as well as with years. After all, 12008 is a valid year, but I would guess that for Nick's purposes, it would not be something he would expect. (You must be careful with the validation you use ... for example, passing the string "02/02/12008" and using dateformat with the mask "dd/mm/yy" returns a date representing February 2, 2008. Using "dd/mm/yyyy" returns February 2, 12008.)
                    • 7. Re: date problem
                      -==cfSearching==- Level 4
                      Agreed about the broad interpretations of IsDate.

                      Dave DuPlantis wrote:
                      > (You must be careful with the validation you use ... for example, passing the string
                      > "02/02/12008" and using dateformat with the mask "dd/mm/yy" returns a date representing
                      > February 2, 2008. Using "dd/mm/yyyy" returns February 2, 12008.)

                      Technically, no. The DateFormat functions expects a date time object and returns a string.

                      I suspect that is probably what was meant. But since the OP may be confused about strings versus date time values it is a point worth clarifying IMO.
                      • 8. Re: date problem
                        murpg
                        BKBK, are you able to help me with this problem I am having looping over an array?
                        http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=7&threadid=1378713
                        • 9. Re: date problem
                          Dave DuPlantis Level 1
                          quote:

                          Originally posted by: -==cfSearching==-
                          Technically, no. The DateFormat functions expects a date time object and returns a string.

                          I suspect that is probably what was meant. But since the OP may be confused about strings versus date time values it is a point worth clarifying IMO.


                          The date/time object can also be passed to DateFormat as a string ... I would say we are discussing a different issue, but I think it's related. It seems as though the OP is struggling with the difference between validation for CF's purposes and validation for the destination's purposes. The ability to pass a string when a date/time object is desired is nice in terms of flexibility, but it also complicates any attempt to explain the situation!
                          • 10. Re: date problem
                            -==cfSearching==- Level 4
                            Dave DuPlantis wrote:
                            > It seems as though the OP is struggling with the difference between validation for CF's
                            > purposes and validation for the destination's purposes.

                            Yes. That is the nature of using date strings versus date/time objects. The interpretation and conversion is handled by the parent application. So the conversion rules are very likely to differ from one application to another and the results may vary. That is why is usually not the best way to transfer date values from CF to a database.

                            As mentioned in earlier responses, the safer method is to use date/time objects, not strings. Then there is no ambiguity and you do not have to concern yourself with whether the database will interpret a date string differently than ColdFusion. For even greater control over how the values are constructed, the createDate and createDateTime functions are useful.

                            > The date/time object can also be passed to DateFormat as a string ...

                            Yes, but it expects / operates on a date/time object. That and the fact that it returns a string, not a date, are subtle but key distinctions.

                            > The ability to pass a string when a date/time object is desired is nice in terms
                            > of flexibility, but it also complicates any attempt to explain the situation!

                            Very true. I think it can also lead to confusion about the usage of some date functions. Take for example DateFormat. The fact that you can also pass in a string, letting CF handle the conversion, seems to give some people the wrong impression about what the function actually does. I read several recent posts where the individual mistakenly thought the DateFormat function converts a string to date/time object, and that the mask tells CF how to interpret the supplied string. It does not. So while this thread was not really about DateFormat .. a small clarification was warranted. Just in case future readers are similarly confused ;-)

                            • 11. Re: date problem
                              musicmp3
                              There are plenty of date functions. If you are thorough the date function is very easy.
                              For music visit http://mp3katalog.eu