CFLib.org – Common Function Library Project

getIsoTimeString(datetime[, convertToUTC])

Last updated August 01, 2013

author

Ben Nadel

Version: 1 | Requires: CF9 | Library: DateLib

Description:
Takes your ColdFusion date/time object and returns a string that represents the date in ISO 8601 complaint format. This separates the date/time values with a "T" and ends with the UTC-marker, "Z". Since it uses the UTC-marker, it will convert your date/time into UTC, unless you tell it not to (using the optional argument).

Return Values:
A date string as per ISO format

Example:

currentTime = now();

// Compare the HTTP time to the ISO time.
writeOutput( "HTTP Time: " & getHttpTimeString( currentTime ) );
writeOutput( "<br />" );
writeOutput( "ISO Time: " & getIsoTimeString( currentTime ) );

Parameters:

Name Description Required
datetime A date/time object Yes
convertToUTC Whether to convert to UTC before formatting (default true) No

Full UDF Source:

/**
 * 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
 * 
 * @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 
 */
string function getIsoTimeString(required date datetime, boolean convertToUTC=true) {
    if (convertToUTC) {
        datetime = dateConvert("local2utc", datetime);
    }

    // When formatting the time, make sure to use "HH" so that the
    // time is formatted using 24-hour time.
    return(
        dateFormat(datetime, "yyyy-mm-dd") &
        "T" &
        timeFormat(datetime, "HH:mm:ss") &
        "Z"
    );
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson