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>