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

How to format a date?

Enthusiast ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

I have tried the following for formatting a date.

#DateFormat(cfData.data.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

Views

471

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
Advocate ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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.date_modified#">
#DateFormat(cfData.data.date_modified, "mm/dd/yyyy")#

Then show us a screen capture of the result.

Cheers
Eddie

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
Enthusiast ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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.date_modified#">

<h6>Line 3: #DateFormat(cfData.data.date_modified, "mm/dd/yyyy")#</h6>

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
Advocate ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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#">

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

Cheers

Eddie

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
Enthusiast ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

Here's what it shows for:

Line 1:<cfdump var="#i#">  

Line 2:<cfdump var="#cfData#">  

Line 3:<cfdump var="#cfData.data#">  

Line 4:#DateFormat(cfData.data.date_modified, "mm/dd/yyyy")#

coldfusion-formatting-date-error-troubleshoot.png

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
Advocate ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

What is the data type of cfData.data.date_modified?

Do you get an error if you do the following:

<cfset dDate = cfData.data.date_modified>

#dateFormat(dDate, 'mm/dd/yyyy')#

Cheers

Eddie

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
Enthusiast ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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.

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
Advocate ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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

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
Advocate ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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

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
Enthusiast ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

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?

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
Advocate ,
Jul 25, 2016 Jul 25, 2016

Copy link to clipboard

Copied

LATEST

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

Cheers

Eddie

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