ColdFusion createdate()

Win*_*aga 3 coldfusion date

我刚学习ColdFusion功能CreateDate.但是,当我使用CreateDate时,值输出是不同的.我的意思是它每个月和每天都在变化.

<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>
Run Code Online (Sandbox Code Playgroud)

txtBirthDate1983年7月10日,但价值 valueOf_txtBirthDateCreateDate创建是1983年07月10.七月为什么?它应该是10月:07(日期),10(月),1983(年).

格式有问题吗?

Ada*_*ron 7

您正createDate()以非常复杂的方式使用.txtBirthDate不是日期,因此您不应将其用作日期函数等的输入.在使用日期函数时year(),month()输入应该已经是日期对象.

假设您从字符串开始,07-10-1983格式为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"));
Run Code Online (Sandbox Code Playgroud)

(显然在实际代码中你没有那些仅用于陈述显而易见的评论!)

只有dateFormat()在输出时才使用最后一点.对于其他日期操作,请使用实际日期对象:birthDate.