Convert and parse ISO 8601 date value - Node.js Date

Node.js examples for Date:Parse

Description

Convert and parse ISO 8601 date value

Demo Code


Date.ISO8601PartMap = {//from  www .ja v  a 2  s . co  m
  Year : 1,
  Month : 3,
  Date : 5,
  Hours : 7,
  Minutes : 8,
  Seconds : 9
};
Date.matchISO8601 = function(a) {
  return a
      .match(/^(\d{4})(-?(\d{2}))?(-?(\d{2}))?(T(\d{2}):?(\d{2})(:?(\d{2}))?)?(Z?(([+\-])(\d{2}):?(\d{2})))?$/)
};
Date.parseISO8601 = function(e) {
  var b = this.matchISO8601(e);
  if (b) {
    var a = new Date, c, d = 0;
    for ( var f in this.ISO8601PartMap) {
      if (part = b[this.ISO8601PartMap[f]]) {
        a["set" + f]((f == "Month") ? parseInt(part) - 1
            : parseInt(part))
      } else {
        a["set" + f]((f == "Date") ? 1 : 0)
      }
    }
    if (b[11]) {
      d = (parseInt(b[14]) * 60) + parseInt(b[15]);
      d *= ((parseInt[13] == "-") ? 1 : -1)
    }
    d -= a.getTimezoneOffset();
    a.setTime(a.getTime() + (d * 60 * 1000));
    return a
  }
};

Related Tutorials