Nodejs Date Parse parseDate(date, format)

Here you can find the source of parseDate(date, format)

Method Source Code

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);/*from  w  w w.j a v  a 2  s. co m*/
      date.setMinutes(0);
      date.setSeconds(0);
      date.setMilliseconds(0);

      for (var i=0; i<=formatParts.length; i++) {
         switch(formatParts[i]) {
            case 'dd':
            case 'd':
            case 'j':
            date.setDate(parseInt(parts[i], 10)||1);
            break;

            case 'mm':
            case 'm':
            date.setMonth((parseInt(parts[i], 10)||1) - 1);
            break;

            case 'yy':
            case 'y':
            date.setFullYear(2000 + (parseInt(parts[i], 10)||1));
            break;

            case 'yyyy':
            case 'Y':
            date.setFullYear(parseInt(parts[i], 10)||1);
            break;
         }
      }
   }

   return date;
};

Related

  1. parse(iSeptaTime)
    Date.parse = function(iSeptaTime) {
      if (iSeptaTime instanceof Date) {
        return iSeptaTime;
      } else {
        var parts = iSeptaTime.match(/(\d+):(\d+)\s+(AM|PM)/);
        if (parts[1] < 12 && parts[3] == 'PM') {
          parts[1] = parseInt(parts[1], 10) + 12;
        } else if (parts[1] == 12 && parts[3] == 'AM') {
          parts[1] = 0;
    ...
    
  2. 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;
    };
    
  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. setISODuration(input)
    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') {
        return console.log("err");
      if(ISODurationRegex.test(input) === false) {
        return console.log('err');
      var matches = input.match(ISODurationRegex);
    ...