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 normal ColdFusion timestamp format when using the #Now()#
notation
... so in this format: {ts '2012-05-17 17:35:44'}
How can I do this?
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>
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"
) />
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>