转换日期/时间ColdFusion

时间:2012-05-17 21:43:53

标签: coldfusion datetime-format iso8601

我正在使用以ISO 8601格式显示日期和时间的脚本,如下所示:2012-05-17T17:35:44.000Z

但是我希望在使用#Now()#表示法时以正常的ColdFusion时间戳格式显示 ......所以采用这种格式:{ts '2012-05-17 17:35:44'}

我该怎么做?

5 个答案:

答案 0 :(得分:5)

自CF 10起,parseDateTime直接支持ISO-8601。

<cfset string = "1997-07-16T19:20:30+01:00">
<cfset date = parseDateTime(string, "yyyy-MM-dd'T'HH:mm:ssX")>

Runnable Example on TryCF.com

答案 1 :(得分:1)

非常确定只是一个解析然后输出会以你想要的格式给你:

#parseDateTime(REReplace("2012-05-17T17:35:44.000Z", "(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*", "\1-\2-\3 \4"))#

编辑:修复并测试过。 ;)

答案 2 :(得分:1)

我认为这是一个更完整的解决方案和优雅的解决方案:

        <cffunction name="ConvertISOToDateTime" access="private" returntype="date">
    <cfargument name="ISODateString" required="yes" type="string" hint="Properly formed ISO-8601 dateTime String">
    <cfscript>
        // time formats have 2 ways of showing themselves: 1994-11-05T13:15:30Z UTC format OR 1994-11-05T08:15:30-05:00
        local.initial_date = parseDateTime(REReplace(ISODateString, "(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*", "\1-\2-\3 \4"));
        // If not in UTC format then we need to
        if (right(arguments.ISODateString, 1) neq "Z") {
            local.timeModifier = "";
            //Now we determine if we are adding or deleting the the time modifier.
            if (ISODateString contains '+' and listlen(listrest(ISODateString,"+"),":") eq 2){
                local.timeModifier = listrest(ISODateString,"+");
                local.multiplier = 1; // Add
            } else if (listlen(listlast(ISODateString,"-"),":") eq 2) {
                local.timeModifier = listlast(ISODateString,"-");
                local.multiplier = -1; // Delete
            }
            if (len(local.timeModifier)){
                local.initial_date = dateAdd("h", val(listfirst(local.timeModifier,":"))*local.multiplier,local.initial_date);
                local.initial_date =  dateAdd("m", val(listlast(local.timeModifier,":"))*local.multiplier,local.initial_date);
            }
        }
        return local.initial_date;
    </cfscript>
</cffunction>

答案 3 :(得分:0)

该日期字符串采用ISO格式,有一个很好的例子,说明如何将其转换为CF日期对象here

...
<cfreturn ARGUMENTS.Date.ReplaceFirst(
    "^.*?(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*$",
    "$1-$2-$3 $4"
    ) />

答案 4 :(得分:0)

使用createOdbcDate功能。最好在查询中进行比较。

<cfquery name="GetVisits" >
    SELECT v.ExecutiveID, eu.firstname, eu.lastname from Visits where
v.visitDate between #CreateODBCDate(DateFrom)#
        AND  #CreateODBCDate(DateTo)#
</cfquery>