CFLib.org – Common Function Library Project

DateConvertISO8601(ISO8601dateString, targetZoneOffset)

Last updated September 28, 2004

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

 
Rated 2 time(s). Average Rating: 4.0

Description:
This function take a string that holds a date in ISO 8601 and converts it to ODBC datetime, but could be adapted to convert to whatever you like. It also will convert to a datetime in a timezone of your choice by specifying the offset, i.e. it could take a datetime in GMT and convert to PT. See http://www.w3.org/TR/NOTE-datetime for description of ISO 8601, the International Standard for the representation of dates and times.

Return Values:
Returns a datetime.

Example:

view plain print about
<cfscript>
    dt = "2004-09-23T09:12:00-07:00";
    PSTtargetZoneOffset = -8;
    ESTtargetZoneOffset = -5;
    GMTtargetZoneOffset = 0;

    TimeZoneInfo = GetTimeZoneInfo();
    if ( TimeZoneInfo.isDSTOn ) {
        PSTtargetZoneOffset = PSTtargetZoneOffset  + 1;
        ESTtargetZoneOffset = ESTtargetZoneOffset  + 1;
    }
</cfscript>
<cfoutput>
    <h2>isDSTOn: #TimeZoneInfo.isDSTOn#</h2>
    <hr>
    <b>#variables.dt#</b><br>
    Formatted with CreateODBCDateTime: #CreateODBCDateTime( left(variables.dt,10) & " " & mid(variables.dt,12,8))#<br>
    <!--- String: #(left(variables.dt,10) & " " &  mid(variables.dt,12,8))#<br>
    DateConvert: #DateConvert("utc2Local", left(variables.dt,10) & " " & mid(variables.dt,12,8))#<br> --->

    DateConvertISO8601 PST: #DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset),"hh:mm:ss tt")#<br>
    DateConvertISO8601 EST: #DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset),"hh:mm:ss tt")#<br>
    DateConvertISO8601 UTC: #DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset),"hh:mm:ss tt")#
</cfoutput>
<hr>
<cfset dt = "2004-09-23T09:12:00+07:00">
<cfoutput>
    <b>#variables.dt#</b><br>
    Formatted with CreateODBCDateTime: #CreateODBCDateTime( left(variables.dt,10) & " " & mid(variables.dt,12,8))#<br>
    <!--- String: #(left(variables.dt,10) & " " &  mid(variables.dt,12,8))#<br>
    DateConvert: #DateConvert("utc2Local", left(variables.dt,10) & " " & mid(variables.dt,12,8))#<br> --->

    DateConvertISO8601 PST: #DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset),"hh:mm:ss tt")#<br>
    DateConvertISO8601 EST: #DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset),"hh:mm:ss tt")#<br>
    DateConvertISO8601 UTC: #DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset),"hh:mm:ss tt")#
</cfoutput>
<hr>
<cfset dt = "2004-09-23T09:12:00Z">
<cfoutput>
    <b>#variables.dt#</b><br>
    Formatted with CreateODBCDateTime: #CreateODBCDateTime( left(variables.dt,10) & " " & mid(variables.dt,12,8))#<br>
    <!--- String: #(left(variables.dt,10) & " " &  mid(variables.dt,12,8))#<br>
    DateConvert: #DateConvert("utc2Local", left(variables.dt,10) & " " & mid(variables.dt,12,8))#<br> --->

    DateConvertISO8601 PST: #DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.PSTtargetZoneOffset),"hh:mm:ss tt")#<br>
    DateConvertISO8601 EST: #DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.ESTtargetZoneOffset),"hh:mm:ss tt")#<br>
    DateConvertISO8601 UTC: #DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset)# : #DateFormat(DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset),"mmm dd, yyyy")# #TimeFormat(DateConvertISO8601(variables.dt,variables.GMTtargetZoneOffset),"hh:mm:ss tt")#
</cfoutput>

Parameters:

Name Description Required
ISO8601dateString The ISO8601 date string. Yes
targetZoneOffset The timezone offset. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Convert a date in ISO 8601 format to an ODBC datetime.
 * 
 * @param ISO8601dateString      The ISO8601 date string. (Required)
 * @param targetZoneOffset      The timezone offset. (Required)
 * @return Returns a datetime. 
 * @author David Satz (david_satz@hyperion.com) 
 * @version 1, September 28, 2004 
 */

function DateConvertISO8601(ISO8601dateString, targetZoneOffset) {
    var rawDatetime = left(ISO8601dateString,10) & " " & mid(ISO8601dateString,12,8);
    
    // adjust offset based on offset given in date string
    if (uCase(mid(ISO8601dateString,20,1)) neq "Z")
        targetZoneOffset = targetZoneOffset -  val(mid(ISO8601dateString,20,3)) ;
    
    return DateAdd("h", targetZoneOffset, CreateODBCDateTime(rawDatetime));

}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

CF Ninja CF Ninja added
calcIRR
10 day(s) ago

Stephen Withington Stephen Withington added
getWeekOfMonth
10 day(s) ago

Adam Cameron Adam Cameron added
createPrimeNumbe...
a while ago

Ray Ford Ray Ford added
timeZoneNow
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson