Date/Time Conversion ColdFusion

白昼怎懂夜的黑 提交于 2019-12-01 18:51:37

As of CF 10, ISO-8601 is supported directly by parseDateTime.

<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

Pretty sure just a parse and then output will give it to you in the format you want :

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

Edit: Fixed and tested. ;)

I think this is a more complete solution and elegant solution :

        <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>
pat

That date string is in ISO format, there is a good example of how to convert it to a CF date object here:

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

Use createOdbcDate function. It is best to compare in query.

<cfquery name="GetVisits" >
    SELECT v.ExecutiveID, eu.firstname, eu.lastname from Visits where
v.visitDate between #CreateODBCDate(DateFrom)#
        AND  #CreateODBCDate(DateTo)#
</cfquery>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!