Skip to content

Fast RFC 3339 date processing in javascript

by Topper on August 13th, 2009

At Motionbox, we use a RFC 3339 time format in some data we return. Javascript doesn't natively handle this format with Date.parse. The only other blog post I've seen on the subject is this:

http://dansnetwork.com/2008/11/01/javascript-iso8601rfc3339-date-parser/

However, since that's a regular expression that's parsing on the string, it can sometimes be slower (but not toooo bad... 1000 iterations on the code from the blog post above took < 100 miliseconds in IE7 (~40 miliseconds in Firefox on a macbook pro).

I knew we could do better with splits. With my code below I got it operating 60% faster (so operations take 40% of the time from the code above).

JavaScript:
  1. Date.prototype.setRFC3339 = function(dString){ 
  2.                 var utcOffset, offsetSplitChar;
  3.                 var offsetMultiplier = 1;
  4.                 var dateTime = dString.split("T");
  5.                 var date = dateTime[0].split("-");
  6.                 var time = dateTime[1].split(":");
  7.                 var offsetField = time[time.length - 1];
  8.                 var offsetString;
  9.                 offsetFieldIdentifier = offsetField.charAt(offsetField.length - 1);
  10.                 if (offsetFieldIdentifier == "Z") {
  11.                     utcOffset = 0;
  12.                     time[time.length - 1] = offsetField.substr(0, offsetField.length - 2);
  13.                 } else {
  14.                     if (offsetField[offsetField.length - 1].indexOf("+") != -1) {
  15.                         offsetSplitChar = "+";
  16.                         offsetMultiplier = 1;
  17.                     } else {
  18.                         offsetSplitChar = "-";
  19.                         offsetMultiplier = -1;
  20.                     }
  21.                     offsetString = offsetField.split(offsetSplitChar);
  22.                     time[time.length - 1] == offsetString[0];
  23.                     offsetString = offsetString[1].split(":");
  24.                     utcOffset = (offsetString[0] * 60) + offsetString[1];
  25.                     utcOffset = utcOffset * 60 * 1000;
  26.                 }
  27.                
  28.                 this.setTime(Date.UTC(date[0], date[1] - 1, date[2], time[0], time[1], time[2]) + (utcOffset * offsetMultiplier ));
  29.                 return this;
  30.             };

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS