Kyle-Falconer
12/13/2013 - 8:40 PM

bad_recurrence.js

var DTSTART = /^DTSTART;[=a-z\/]+:([\-0-9T:]+)$/im;
var DTEND = /^DTEND;[=a-z\/]+:([\-0-9T:]+)$/im;
var RRULE = /^RRULE:FREQ=(?:DAILY|WEEKLY);(?:BYDAY=[A-Z,]+;)?UNTIL=([0-9]+)$/im;

/**
 * @param {string} recurrencedata
 * @return {{startTime:string, endTime:string, endDate:string}}
 */
var ParseRecurrenceDate = function (recurrencedata) {
  var StartTime = DTSTART.exec(recurrencedata)[1];
  var EndTime = DTEND.exec(recurrencedata)[1];
  if (StartTime.indexOf("T") > 0) {
    StartTime = StartTime.substr(0, 4) + "-" + StartTime.substr(4, 2) + "-" + StartTime.substr(6, 2) + "T" + StartTime.substr(9, 2) + ":" + StartTime.substr(11, 2) + StartTime.substr(13, 2);
    EndTime = EndTime.substr(0, 4) + "-" + EndTime.substr(4, 2) + "-" + EndTime.substr(6, 2) + "T" + EndTime.substr(9, 2) + ":" + EndTime.substr(11, 2) + EndTime.substr(13, 2);
  } else {
    StartTime = StartTime.substr(0, 4) + "-" + StartTime.substr(4, 2) + "-" + StartTime.substr(6, 2);
    EndTime = EndTime.substr(0, 4) + "-" + EndTime.substr(4, 2) + "-" + EndTime.substr(6, 2);
  }
  var EndDate = RRULE.exec(recurrencedata)[1];
  EndDate = new Date(parseInt(EndDate.substr(0, 4), 10), parseInt(EndDate.substr(4, 2), 10) - 1, parseInt(EndDate.substr(6, 2), 10));
  EndDate.setDate(EndDate.getDate() - 1);
  EndDate = EndDate.getFullYear().toString() + '-' + (EndDate.getMonth() < 9 ? '0' : '') + (EndDate.getMonth() + 1).toString() + '-' + (EndDate.getDate() < 10 ? '0' : '') + EndDate.getDate().toString();

  return { startTime: StartTime, endTime: EndTime, endDate: EndDate };
};