3

如果我运行查询:

<cfquery name="gGet" datasource="#application.datasource#">
   select dateuploaded from table
</cfquery>

我得到了一个可爱的约会时间,如下所示:

2012-12-01 12:46:00 

但是,如果我创建一个新的查询对象并将日期时间插入其中

<cfset qReturn = queryNew("deployDate", "Date")>
<cfoutput query="qGet" startrow="1" maxrows="7">
    <cfset queryAddRow(qReturn)>
    <cfset querySetCell(qReturn,"deployDate",qGet.dateuploaded)>
</cfoutput>

我得到了这个可怕的时间戳垃圾{ts '2012-12-01 12:46:00'}

我应该怎么做才能保留日期时间?

或者,我将如何将时间戳重新格式化为日期时间?

4

1 回答 1

0

You are almost certainly worrying about nothing. You're simply looking at what the date/time object returns when its toString method is called, which ColdFusion uses to be able to output something on the screen (which needs to be string data). The various values you are looking at will all be completely valid date/time types, just different classes implement toString differently. Your code will be dealing with the object, not the string representation of its value, so this is a non-issue.

Sounds like yer just outputting a raw value from either the DB result or the constructed query without formatting it, thus meaning CF needs to convert it to a string via its default toString method, and as I say above the toString methods vary from one DateTime implementation to another (the class of the object from the DB is different from the one CF uses for its own date objects, hence the different toString implementations you're seeing). One should never do that if one cares about what is being output, as you are here. Always convert a DateTime object to a string yourself if you care about the format used to display it:

<cfoutput>#gGet.dateuploaded[1].dateTimeFormat("yyyy-mm-dd HH:nn:ss")#</cfoutput>

<cfoutput>#qReturn.deployDate[1].dateTimeFormat("yyyy-mm-dd HH:nn:ss")#</cfoutput> (respectively, based on the info you've provided us).

This will return & display the date object values in the format you specify, rather than leaving it to CF to use its defaults.

Only do this for display. For any date operations just leave the things as-is. You're only perceiving there to be something "wrong" here because you don't understand that dates in CFML are objects not just human-readable strings. CF isn't - however - confused by this ;-)

I discuss this with more clarify (and taking up more space) in this article about date/time objects and their string representations, if you want to take a look.

于 2012-12-01T10:03:03.150 回答