Nodejs Date Parse setISODuration(input)

Here you can find the source of setISODuration(input)

Method Source Code

Date.prototype.setISODuration = function (input) {
  var ISODurationRegex = /P((([0-9]*\.?[0-9]*)Y)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)W)?(([0-9]*\.?[0-9]*)D)?)?(T(([0-9]*\.?[0-9]*)H)?(([0-9]*\.?[0-9]*)M)?(([0-9]*\.?[0-9]*)S)?)?/
  
  if(typeof input !== 'string') {
    //throw err//from  www .j  a va2  s.c om
    return console.log("err");
  }

  if(ISODurationRegex.test(input) === false) {
    //Throw err
    return console.log('err');
  }

  var matches = input.match(ISODurationRegex);
  
  var durations = {
    FullYear: parseFloat(matches[3]),
    Month: parseFloat(matches[5]),
    Hours: (function () {
      var weeks = parseFloat(matches[7]) || 0;
      var days = parseFloat(matches[9]) || 0;
      var hours = parseFloat(matches[12]) || 0;
      return hours + days * 24 + weeks * 24 * 7
    }()),
    Minutes: parseFloat(matches[14]),
    Seconds: parseFloat(matches[16])
  };
  
  for(var prop in durations) {
    var getter = "get" + prop;
    var setter = "set" + prop;
    var value = this[getter]() + (durations[prop] || 0);
    
    this[setter](value);
  }
}

Related

  1. parse(s)
    Date.prototype.parse = function (s) {
        if ((s || '') == '')
            return null;
        if (typeof (s) == "object")
            return s;
        if (typeof (s) == 'string') {
            if (/\/Date\(.*\)\
                return eval(s.replace(/\/Date\((.*?)\)\
            else if (/(\d{8})/.test(s)) {
                return eval(s.replace(/(\d{4})(\d{2})(\d{2})/, "new Date($1,parseInt($2)-1,$3)"));
            else if (/(\d{4})[-/](\d{1,2})[-/](\d{1,2})[T\s](\d{1,2})\:(\d{1,2})(?:\:(\d{1,2}))?/.test(s)) {
                return eval(s.replace(/(\d{4})[-/](\d{1,2})[-/](\d{1,2})[T\s](\d{1,2})\:(\d{1,2})(?:\:(\d{1,2}))?[\w\W]*/, "new Date($1,parseInt($2)-1,parseInt($3),parseInt($4),parseInt($5),parseInt($6)||0)"));
            else if (/(\d{4})[-/](\d{1,2})[-/](\d{1,2})/.test(s)) {
                return eval(s.replace(/(\d{4})[-/](\d{1,2})[-/](\d{1,2})/, "new Date($1,parseInt($2)-1,$3)"));
            try {
                return new Date(s);
            } catch (e) {
                return null;
        return null;
    };
    
  2. parseDate(date, format)
    Date.parseDate = function(date, format) {
      if (format === undefined)
        format = 'Y-m-d';
      var formatSeparator = format.match(/[.\/\-\s].*?/);
      var formatParts     = format.split(/\W+/);
      var parts           = date.split(formatSeparator);
      var date            = new Date();
      if (parts.length === formatParts.length) {
        date.setHours(0);
    ...
    
  3. parseFromJsonFormat(dateStr)
    Date.parseFromJsonFormat = function(dateStr) {
        var str = dateStr.substring(6, dateStr.length - 2);
        return new Date(str * 1);
    };
    
  4. setISO8601(timestamp)
    Date.prototype.setISO8601 = function (timestamp) {
     var match = timestamp.match(
      "^([-+]?)(\\d{4,})(?:-?(\\d{2})(?:-?(\\d{2})" +
      "(?:[Tt ](\\d{2})(?::?(\\d{2})(?::?(\\d{2})(?:\\.(\\d{1,3})(?:\\d+)?)?)?)?" +
      "(?:[Zz]|(?:([-+])(\\d{2})(?::?(\\d{2}))?)?)?)?)?)?$");
     if (match) {
      for (var ints = [2, 3, 4, 5, 6, 7, 8, 10, 11], i = ints.length - 1; i >= 0; --i)
       match[ints[i]] = (typeof match[ints[i]] != "undefined"
        && match[ints[i]].length > 0) ? parseInt(match[ints[i]], 10) : 0;
    ...
    
  5. setISO8601(timestamp)
    Date.prototype.setISO8601 = function (timestamp) {
        var match = timestamp.match("^([-+]?)(\\d{4,})(?:-?(\\d{2})(?:-?(\\d{2})" +
            "(?:[Tt ](\\d{2})(?::?(\\d{2})(?::?(\\d{2})(?:\\.(\\d{1,3})(?:\\d+)?)?)?)?" +
            "(?:[Zz]|(?:([-+])(\\d{2})(?::?(\\d{2}))?)?)?)?)?)?$");
        if (match) {
            for (var ints = [2, 3, 4, 5, 6, 7, 8, 10, 11], i = ints.length - 1; i >= 0; --i)
                match[ints[i]] = (typeof match[ints[i]] != "undefined" && match[ints[i]].length > 0)
                    ? parseInt(match[ints[i]], 10)
                    : 0;
    ...
    
  6. parseToDate()
    String.prototype.parseToDate = function () {
        var s = this.split("/");
        return s.length === 3 ? new Date(s[0], parseInt(s[1]) - 1, s[2]) : null;
    };
    
  7. parseToDate()
    String.prototype.parseToDate = function() {
      var year = Number(this.substring(0, 4));
      var month = Number(this.substring(5, 7)) - 1;
      var date = Number(this.substring(8, 10));
      var hours = Number(this.substring(11, 13));
      var minutes = Number(this.substring(14, 16));                                                                                                  
        var result = new Date(year, month, date, hours, minutes,0,0);
        console.log(year);
        console.log(month);
    ...
    
  8. parseDate()
    String.prototype.parseDate = function () {
        var monthArray = {'Jan':0, 'Feb':1, 'Mar':2, 'Apr':3, 'May':4, 'Jun':5, 'Jul':6, 'Aug':7, 'Sep':8, 'Oct':9, 'Nov':10, 'Dec':11};
        var dateArray = this.split('-');
        return new Date(dateArray[2], monthArray[dateArray[1]], dateArray[0]);
    };