Copy link to clipboard
Copied
ColdFusion 2021 Update 2 (2021.0.02.328618) WINx64
Java VM Version 11.0.11+9-LTS-194
<cfoutput>
#DateFormat("01/01/1982","dd-Mmm-yyyy")#
</cfoutput>
Thrown the same error with "01-Jan-1982" and all different date format for 01/01/1982.
No issue for dates like 02/01/1982, 01/01/1981, etc.
Had tried to run the same code on cffidle.org but no error was thrown.
A quick check was that cffidle was running on Linux while mine was on Windows.
The error is as below: -
The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.
The following information is meant for the website developer for debugging purposes. | ||||||||||||||||||
Error Occurred While Processing Request | ||||||||||||||||||
|
The DateFormat is working when using LSparseDateTime; but not when using parseDateTime.
By @Tan Wee Han
Thanks for sharing that, @Tan Wee Han . I am not surprised that you can still get an error. The reason is the same as the one I explained before.
When you request ColdFusion to cast from string to date, as LSparsedatetime and parsedatetime do, there is always a risk. ColdFusion might not recognize the string as a valid date. Whether or not the string is a valid date depends on a number
...Copy link to clipboard
Copied
I tested on TryCF.com (which has more CF versions to test) and it worked without throwing an error.
I wonder if the problem is the mask. Is there any reason you aren't using the "dd-mmm-yyyy" syntax?
Please note that capital letters may mean something else as Adobe changed how things work in late 2020.
https://helpx.adobe.com/coldfusion/kb/dateformat-function-coldfusion-2021.html
Also: CF2016u3 (Oct 2016) added some additional masks:
Copy link to clipboard
Copied
Though ColdFusion is weakly-typed, there are times when it enforces the rules strictly. You got the error because your code is not quite correct. In fact, ColdFusion tells you precisely why: "The value of parameter 1, which is currently 01/01/1982, must be a class java.util.Date value." In other words, you have used a string as the first argument, but the correct type to use is a date object.
ColdFusion will often do its best to cast a non-date argument (in this case, a string) to a date. Sometimes it can't, such as in this case. I suspect that ColdFusion had trouble casting "01/01/1982" to a date. The result is then a parameter validation error.
Suggestion:
<cfset dateObject=parseDatetime("01/01/1982")>
<!--- Alternative, based on your locale --->
<!---<cfset dateObject=LSparseDatetime("01/01/1982")>--->
<cfoutput>
#DateFormat(dateObject,"dd-Mmm-yyyy")#
</cfoutput>
Copy link to clipboard
Copied
The DateFormat is working when using LSparseDateTime; but not when using parseDateTime.
Thanks.
Copy link to clipboard
Copied
The DateFormat is working when using LSparseDateTime; but not when using parseDateTime.
By @Tan Wee Han
Thanks for sharing that, @Tan Wee Han . I am not surprised that you can still get an error. The reason is the same as the one I explained before.
When you request ColdFusion to cast from string to date, as LSparsedatetime and parsedatetime do, there is always a risk. ColdFusion might not recognize the string as a valid date. Whether or not the string is a valid date depends on a number of factors, the commonest being the ColdFusion version, the Java version on which ColdFusion is running, the Locale, and the encoding.
Which is why there are functions to test whether a string is a valid representation of a date. The idea is that you test the string first.
<cfoutput>
isValid("date", "01/01/1982"): #isValid("date", "01/01/1982")# <br>
isValid("date", "1/1/1982"): #isValid("date", "1/1/1982")# <br>
isDate("01/01/1982"): #isDate("01/01/1982")# <br>
isDate("1/1/1982"): #isDate("1/1/1982")# <br><br>
Locale: #getLocale()# <br>
LSisDate("01/01/1982"): #LSisDate("01/01/1982")# <br>
LSisDate("1/1/1982"): #LSisDate("1/1/1982")# <br>
</cfoutput>
If you get a yes, then it will probably be safe to assume ColdFusion can cast the string to a date. As you can see, I said probably.
If you want 100% reliability, then avoid representing dates as strings. Use instead createdate or createdatetime.
<cfset dateObject=createdate(1982,1,1)>
<!--- Alternative --->
<!---<cfset dateObject=createdatetime(1982,1,1,0,0,0)>--->
<cfoutput>
#DateFormat(dateObject,"dd-Mmm-yyyy")#
</cfoutput>