Date/Time Conversion ColdFusion

前端 未结 5 613
名媛妹妹
名媛妹妹 2021-01-19 13:17

I\'m working with a script that displays the date and time in ISO 8601 format like so: 2012-05-17T17:35:44.000Z.

but I would like it to display in the n

相关标签:
5条回答
  • 2021-01-19 13:34

    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

    0 讨论(0)
  • 2021-01-19 13:37

    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>
    
    0 讨论(0)
  • 2021-01-19 13:38

    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"
        ) />
    
    0 讨论(0)
  • 2021-01-19 13:40

    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. ;)

    0 讨论(0)
  • 2021-01-19 13:40

    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>
    
    0 讨论(0)
提交回复
热议问题