2

I am moving one of our applications from ColdFusion 9.01 to ColdFusion 11 and encountered a situation where I cannot get the date formatted the way I want it using "DateFormat". I read through the docs since things have changed in CF versions, but I honestly can't figure out why this isn't working. It worked beautifully in CF 9. I know it's probably something very easy, but I am just not seeing it.

The query (Oracle DB) provides me a list of the last 30 days and the loop is simply to reformat the date output from "2014-07-01 00:00:00.0" to a more friendly looking display of 01-Jul-2014 except that I cannot get it to format as "dd-mmm-yyyy" it just spits back the original output from the query. I hard coded the date where normally there would be a cfquerparam. Any ideas?

<cfquery name="qryDateArray" datasource="#request.db#">
  select trunc(to_date('07/01/2014', 'mm/dd/yyyy') + 1 - rownum) as ref_date
  from dual connect by rownum <= 30
</cfquery>

<cfloop from="1" to="#qryDateArray.recordcount#" index="j">
  <cfset qryDateArray.ref_date[j] = DateFormat(qryDateArray.ref_date[j], "dd-mmm-yyyy")>
</cfloop>

<cfoutput>
  <cfdump var="#qryDateArray#">
</cfoutput>
4

2 回答 2

1

如果您给我们一个可移植的测试用例而不是依赖于您的数据库的测试用例,那就太好了,但我怀疑这是因为 ColdFusion 在查询列的类型管理方面变得更加严格。

因此 CF 认为您的ref_date列是日期类型,因此当您尝试将格式化的字符串放回查询列时,CF 会尝试(并成功)将字符串转换回日期。

在旁边:

我不得不想知道为什么你不从一开始就格式化数据库中的数据字符串,只是按照你需要的方式返回它,而不是返回其他东西,然后循环处理它来调整它..?

于 2014-07-10T14:54:51.297 回答
1

我无法在 CF11 上测试它,因为我手边没有它。当我在我的 CF10 环境中运行它时,我确实验证了您的代码虽然返回了结果,但正如您所解释的那样。因此,您可以做的是向查询对象添加一列并将其定义为 varchar 并将您的格式化数据添加到其中。这反过来又转储了格式化的日期。

<cfquery name="qryDateArray" datasource="#request.db#">
  select trunc(to_date('07/01/2014', 'mm/dd/yyyy') + 1 - rownum) as ref_date
  from dual connect by rownum <= 30
</cfquery>

<cfset aryData  = [] />

<cfloop from="1" to="#qryDateArray.recordcount#" index="j">
  <cfset ArrayAppend(aryData, DateFormat(qryDateArray.ref_date[j], "dd-mmm-yyyy")) />
</cfloop>

<cfset QueryAddColumn(qryDateArray, "STRDATE", "VarChar", aryData) />

<cfoutput>
  <cfdump var="#qryDateArray#">
</cfoutput>

在此处输入图像描述

如果依赖于查询列名,则可以使用此处解释的 Ben 方法对列进行一些重命名: http ://www.bennadel.com/blog/357-ask-ben-sharing-coldfusion-query-column-名称.htm

于 2014-07-10T16:42:36.163 回答