CFLib.org – Common Function Library Project

ISODateToTS(str)

Last updated October 22, 2012

Version: 2 | Requires: ColdFusion 5 | Library: DateLib

 
Rated 1 time(s). Average Rating: 5.0

Description:
Takes a text string of an ISO date and converts it to a datetime object. This is useful when parsing an RDF or RSS feed that uses Dublin Core or otherwise uses ISO formatted dates. Strips the date time down to the hour and minute, omitting seconds (as some feeds do not use seconds). Please note that the tiem zone is ignored.

Return Values:
Returns a datetime

Example:

view plain print about
<cfoutput>
<cfset s = "1994-11-05T08:15:30Z">
s: #s#<br />
ISODateToTS(): #ISODateToTS(s)#
<hr />
<cfset s = "1994-11-05T08:15:30+05:00">
s: #s#<br />
ISODateToTS(): #ISODateToTS(s)#
<hr />
<cfset s = "1994-11-05T08:30:30-08:30">
s: #s#<br />
ISODateToTS(): #ISODateToTS(s)#
<hr />
<cfset s = "1994-11-05T08:30:30-08:00">
s: #s#<br />
ISODateToTS(): #ISODateToTS(s)#
<hr />
</cfoutput>

Parameters:

Name Description Required
str ISO datetime string to parse. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Converts text string of ISO Date to datetime object; useful for parsing RSS and RDF.
 * * version 1.0 by James Edmunds
 * * version 2.0 by Adam Reynolds (added support for TZ offsets)
 * * version 2.1 by Adam Cameron (merged James's and AdamR's versions &amp; fixed bug with offsets that had a minute component to them)
 * 
 * @param str      ISO datetime string to parse. (Required)
 * @return Returns a datetime 
 * @author James Edmunds (jamesedmunds@jamesedmunds.com) 
 * @version 2, October 22, 2012 
 */

function ISODateToTS(str) {
    // time formats have 2 ways of showing themselves: 1994-11-05T13:15:30Z UTC format OR 1994-11-05T08:15:30-05:00
    var initialDate        = parseDateTime(REReplace(arguments.str, "(\d{4})-?(\d{2})-?(\d{2})T([\d:]+).*""\1-\2-\3 \4"));
    var timeModifier    = "";
    var multiplier        = 0;

    // If not in UTC format then we need to offset the time
    if (right(arguments.str, 1) neq "Z"{
        //Now we determine if we are adding or deleting the the time modifier.
        if (arguments.str contains '+' and listlen(listrest(arguments.str, "+"), ":"eq 2){
            timeModifier = listRest(arguments.str,"+");
            multiplier = 1; // Add
        } else if (listlen(listLast(arguments.str,"-"),":"eq 2) {
            timeModifier = listLast(arguments.str,"-");
            multiplier = -1; // Delete
        }
        if (len(timeModifier)){
            initialDate = dateAdd("h"val(listFirst(timeModifier, ":"))*multiplier, initialDate);
            initialDate =  dateAdd("n"val(listLast(timeModifier, ":"))*multiplier, initialDate);
        }
    }
    return initialDate;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
composeDateTime
14 day(s) ago

Chris Weller Chris Weller added
convertQueryStri...
a while ago

Greg Nettles Greg Nettles added
arrayDiff
a while ago

Nathan Dintenfass Nathan Dintenfass added
ArrayOfStructsSo...
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 36 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Rachel Lehman deAccent
Rated 5.0, 6 time(s)

Isaac Dealey                                      countArbitraryDa...
Rated 5.0, 5 time(s)

Created by Raymond Camden / Design by Justin Johnson