Convert ColdFusion Date and Time

I work with a script, which displays the date and time format in ISO 8601 as follows: 2012-05-17T17:35:44.000Z.

but I would like it to display in the standard timestamp ColdFusion format when using notation #Now()# ... so in this format:{ts '2012-05-17 17:35:44'}

How can i do this?

+3
source share
5 answers

As with 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 at TryCF.com

+5
source

Quite accurately, only parsing, and then the output will give it to you in the desired format:

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

Edit: Fixed and tested .;)

+1
source

, :

        <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>
+1

This 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"
    ) />
0
source

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

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

All Articles