Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active December 4, 2022 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/5985859240f14ce8b73c1897fcc16970 to your computer and use it in GitHub Desktop.
Save JamoCA/5985859240f14ce8b73c1897fcc16970 to your computer and use it in GitHub Desktop.
<cfscript>
/**
* Converts your date into a time string the aheres to the ISO 8601 standard (for use with some API calls).
* v1.0 by Ben Nadel
* https://cflib.org/udf/getIsoTimeString
* @param datetime A date/time object (Required)
* @param convertToUTC Whether to convert to UTC before formatting (default true) (Optional)
* @return A date string as per ISO format
* @author Ben Nadel (ben@bennadel.com)
* @version 1.0, August 1, 2013
* @version 2.0 May 10, 2017 by James Moberg/SunStar Media; Return empty string for non-date values. Return UTC offset (instead of Z).
* Tested CF2016+ and Lucee
* Gist: https://gist.github.com/JamoCA/5985859240f14ce8b73c1897fcc16970
*/
public string function getISOTimeString(string datetime, boolean convertToUTC=true) output=false hint="Converts your date into a time string the adheres to the ISO 8601 standard" {
if (!isvalid("date", arguments.datetime)){
return "";
}
local.mask = "yyyy-mm-dd'T'HH:nn:ssZ";
if (arguments.convertToUTC) {
/* ACF doesn't like empty timezone values. Only GMT works, not UTC. */
return replace(datetimeformat(arguments.datetime, local.mask, "gmt"), "+0000", "Z");
}
return replace(datetimeformat(arguments.datetime, local.mask), "+0000", "Z");
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment