10 Replies Latest reply on Jul 25, 2016 9:47 AM by EddieLotter

    How to format a date?

    2Charlie Level 1

      I have tried the following for formatting a date.

       

      #DateFormat(cfData.data[i].date_modified, "mm/dd/yyyy")#

       

      This above formatting shows this Error in custom script instead. This is what the date looks like: 07/20/2016 2:48 pm CDT

        • 1. Re: How to format a date?
          EddieLotter Level 3

          Put the following in the ColdFusion script, above the problem line, which is line 3 below:

          #DateFormat(createDateTime(2016, 7, 20, 14, 48, 0), "mm/dd/yyyy")#
          <cfdump var="#cfData.data[i].date_modified#">
          #DateFormat(cfData.data[i].date_modified, "mm/dd/yyyy")#
          

           

          Then show us a screen capture of the result.

           

          Cheers
          Eddie

          • 2. Re: How to format a date?
            2Charlie Level 1

            Thanks for the response. This is what it shows.

            coldfusion-formatting-date-error.png

            Here's the code:

            <h6>Modify Date: #DateFormat(createDateTime(2016, 7, 20, 14, 48, 0), "mm/dd/yyyy")#</h6>
            This is the dump:<cfdump var="#cfData.data[i].date_modified#">
            <h6>Line 3: #DateFormat(cfData.data[i].date_modified, "mm/dd/yyyy")#</h6>
            
            • 3. Re: How to format a date?
              EddieLotter Level 3

              Okay, let's have a look at each piece. Show us the result of the following:

              <cfdump var="#i#"> 
              <cfdump var="#cfData#"> 
              <cfdump var="#cfData.data[i]#"> 
              #DateFormat(cfData.data[i].date_modified, "mm/dd/yyyy")#
              

               

              Cheers

              Eddie

              • 4. Re: How to format a date?
                2Charlie Level 1

                Here's what it shows for:

                 

                Line 1:<cfdump var="#i#">   
                Line 2:<cfdump var="#cfData#">   
                Line 3:<cfdump var="#cfData.data[i]#">   
                Line 4:#DateFormat(cfData.data[i].date_modified, "mm/dd/yyyy")#
                

                 

                coldfusion-formatting-date-error-troubleshoot.png

                • 5. Re: How to format a date?
                  EddieLotter Level 3

                  What is the data type of cfData.data[i].date_modified?

                   

                  Do you get an error if you do the following:

                  <cfset dDate = cfData.data[i].date_modified>
                  #dateFormat(dDate, 'mm/dd/yyyy')#
                  

                   

                  Cheers

                  Eddie

                  • 6. Re: How to format a date?
                    2Charlie Level 1

                    I don't know the data type. We're getting our data from JSONP from third party. I'll ask them. But yes, this line #dateFormat(dDate, 'mm/dd/yyyy')# of code does generate the error. Line 1 did not cause an error though.

                    • 7. Re: How to format a date?
                      EddieLotter Level 3

                      I strongly suspect that it is a simple string and CF is choking on the time zone. It cannot convert a string to a date if it includes a time zone.

                       

                      You can ask your source to not provide the time zone, or strip it yourself. If your CF server is in a different time zone than the dates you are dealing with, then you will need to have your source provide you with UTC dates which you can convert to the local time zone of your server, if necessary. See the DateConvert() function in your CF reference.

                       

                      Cheers

                      Eddie

                      • 8. Re: How to format a date?
                        EddieLotter Level 3

                        You can try using the LSParseDateTime() function to convert from a string that includes a time zone to a CF date time object.

                         

                        I have no experience with the function, but it looks promising for your scenario.

                         

                        Basically, the answer to your original question is that the DateFormat() function is failing to correctly interpret the string formatted date that you are giving it.

                         

                        Cheers

                        Eddie

                        • 9. Re: How to format a date?
                          2Charlie Level 1

                          Thanks, Eddie for all the help. I'll ask them. So, if I want to strip out the time, all I have to do is strip out the space plus the CDT from the field and then convert that to date and then format it the way I want, correct?

                          • 10. Re: How to format a date?
                            EddieLotter Level 3

                            Yes, you can do that, but if time zones are important then look into the function I mentioned.

                             

                            Cheers

                            Eddie