seUser
seUser

Reputation: 1103

How to allow regex to work for mm/dd/yyyy and yyyy-mm-dd format

My regex for mm/dd/yyyy is:

/^((0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)[0-9]{2})*$/

I want to use this for mm/dd/yyyy and yyyy-mm-dd both format.

I tried below one also but don't want user to type in any number for date and month.

/^((\d{4})-(\d{2})-(\d{2})|(\d{2})\/(\d{2})\/(\d{4}))$/

I tried below to make it work but not sure what is wrong here:

/^((0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)[0-9]{2})|((19|20)[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])*$/

Upvotes: 1

Views: 769

Answers (2)

Mike Samuel
Mike Samuel

Reputation: 120586

The below gives you a function that produces a parser for a format like the one you describe. dateParser('yyyy/mm/dd')('1990-03-25') is { y: 1990, m: 3, d: 25 }.

You can also use the chain function thus chain([dateParser('mm/dd/yyyy'), dateParser('yyyy-mm-dd')])('2014-04-04') to try formats in order.

var patterns = {
  yyyy: '\\d{4}',
  yy:   '\\d{2}',
  mm:   '0\\d|1[012]',
  m:    '0?\\d|1[012]',
  dd:   '[3][01]|[012]\\d',
  d:    '[3][01]|[012]?\\d',
  HH:   '[2][0-4]|[01][0-9]',
  H:    '[2][0-4]|[01]?[0-9]',
  hh:   '[1][0-2]|0\\d',
  h:    '[1][0-2]|0?\\d',
  MM:   '[0-5]\\d',
  M:    '[0-5]?\\d',
  ss:   '[0-5]\\d',
  s:    '[0-5]?\\d'
}

function dateParser(pattern) {
  var groupToFieldNames = [null];
  var regexPattern = pattern.replace(
      /([A-Za-z]+)|[.?+*\[\]\(\)\{\}^$\|\\]/g,
      function (m, field, punc) {
        if (patterns.hasOwnProperty(field)) {
          groupToFieldNames.push(field[0]);
          return '(' + patterns[field] + ')';
        } else if (punc) {
          return '\\' + punc;
        } else {
          return m;
        }
      });
  var regex = new RegExp('^' + regexPattern + '$');
  return function (dateString) {
    var match = dateString.match(regex);
    if (match) {
      var fields = {};
      for (var i = 1, n = groupToFieldNames.length; i < n; ++i) {
        fields[groupToFieldNames[i]] = parseInt(match[i], 10);
      }
      return fields;
    }
  };
}

function chain(parsers) {
  var arr = parsers.slice(0);
  return function (x) {
    for (var i = 0, n = arr.length; i < n; ++i) {
      var result = (0,arr[i])(x);
      if (result) { return result; }
    }
  };
}

console.log(JSON.stringify(
    dateParser('yyyy/mm/dd H:MM:ss')('2014/12/31 7:30:25')
));

console.log(JSON.stringify(
    chain([dateParser('mm/dd/yyyy'), dateParser('yyyy-mm-dd')])
    ('2014-04-04')
));

Upvotes: 1

Greg Burghardt
Greg Burghardt

Reputation: 18938

This is all you should need:

var regex = /^\d{2}\/\d{2}\/\d{4}|\d{4}-\d{2}-\d{2}$/;

regex.test("1990-03-25") // returns true
regex.test("01/10/2006") // returns true

Upvotes: 1

Related Questions