Creating ColdFusion ()
I just learned about the ColdFusion feature CreateDate
. But, when I use CreateDate, the value output is different. I mean, it changes every day and every day.
<cfoutput>
<cfset txtBirthDate='07-10-1983'>
<cfset valueOf_txtBirthDate = dateFormat(CreateDate(Year(txtBirthDate),Month(txtBirthDate),Day(txtBirthDate)),'YYYY-MMM-DD')>
#txtBirthDate#<br/><br/>
#valueOf_txtBirthDate#<br/>
</cfoutput>
The txtBirthDate value is 07-10-1983 , but the valueOf_txtBirthDate value , which was CreateDate
created by 1983 -Jul-10 . Why is it July? It is supposed to be October: 07 (date), 10 (month), 1983 (year).
Is there something wrong with the format?
source to share
You are using in a createDate()
very confusing way. txtBirthDate
is not a date, so you shouldn't use it as input to date functions like year()
, month()
etc. At the time you use the date function, the input must already be a date object.
Let's say you start at a line 07-10-1983
where the format is mm-dd-yyyy
.
txtBirthDate ='07-10-1983'; // dd-mm-yyyy
// extract the specific date parts from the string
yyyy = listLast(txtBirthDate, "-");
mm = listGetAt(txtBirthDate, 2, "-");
dd = listFirst(txtBirthDate, "-");
// create a date object out of those components
birthDate = createDate(yyyy, mm, dd);
// output the date object in human readable format
writeOutput(dateFormat(birthDate, "YYYY-MM-DD"));
(Obviously, in real code, you wouldn't have comments like these that only serve to indicate the obvious!)
Use only dateFormat()
at the last point in withdrawal. For other operations with the date using the date of the actual object: birthDate
.
source to share