Skip to content

Fast RFC 3339 date processing in javascript

by Topper on August 13th, 2009

UPDATE: This article is translated to Serbo-Croatianlanguage by Anja Skrba.

UPDATE: Removed dead Motionbox link. I work at Amicus now.

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.             };

  • Disappointed

    Doesn’t seem to work. I get an error because it treats offsetField as an array and it’s just a string. Even after fixing that I get an invalid date time. :(

  • Anonymous

    @disappointed – we were using this in a production environment for a couple of years without problem. Do you have an example? Also – in Javascript, you can use strings as arrays (at least in the browsers).

  • http://twitter.com/thegoleffect Van Nguyen

    Line 22, is that supposed to be “=” or is it really just doing the comparison and skipping (“==”)”==”)

  • Kobydiego

    substr is faster than split (with fixed format)

  • kevin

    this is a big help for working with data coming out of gnip powertrack in the activitystream format. thanks!