0

I need a regular expression in javascript that can test for dates with the following formats:

  • yyyy-MM-dd (Example: 2014-12-31)
  • dd.MM.yyyy (Example: 31.12.2014)
  • dd.MM.yy (Example: 31.12.14)
  • ddMMyyyy (Example: 12312014)
  • ddMMyy (Example: 123114)
  • dd.M.yy (Example: 12.6.14)
  • dd.M.yyyy (Example: 12.6.2014)

Sorry, but I am really terrible at regular expressions. This is probably a breeze for a pro. Thanks a milion.

Johann
  • 27,536
  • 39
  • 165
  • 279
  • One of these is not like the others... Namely, the first one is in a completely different format to the others, making a single regex difficult to use. – Niet the Dark Absol Jun 11 '14 at 15:09
  • 1
    You cannot (and shouldn't) use regular expressions to validate dates. Is `99.33.8888` a valid date? – georg Jun 11 '14 at 15:14
  • The last two examples don't match their signature. (they are MMddyyyy) – user887675 Jun 11 '14 at 15:22
  • `ddMMyyyy (Example: 12312014)` is incorrect: '31' is the day, and '12' is the month, so it should be `MMddyyyy` or the example should be rewritten. `ddMMyy (Example: 123114)` is incorrect: same reason. And if you're consistent with your "Example" formatting, '12' is always representing the MONTH, not the DAY, and either `31` or `6` should be the DAY. Could you modify the post to reflect the correct format (or the correct Example)? – OnlineCop Jun 11 '14 at 16:10

2 Answers2

1

Your first pattern is too different from the others to be meaningfully merged. So: \d{4}-\d\d-\d\d

For the others, you are allowing an optional . as a separator, and either two- or four-digit years. So you have: \d\d(\.?)\d\d?\1\d\d(?:\d\d)?

The \1 in the above is to basically repeat the (\.?)'s result - ie. a dot if there was a dot before, or nothing if not.

Result:

/^(?:\d{4}-\d\d-\d\d|\d\d(\.?)\d\d?\1\d\d(?:\d\d)?)$/
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • So are you saying that your result can only be used for the last 4 examples but not for the first one? I thought regular expressions allowed some kind of ORing. – Johann Jun 11 '14 at 15:13
  • @AndroidDev I broke the problem into two problems, then joined them together with the `|` - it is there ;) – Niet the Dark Absol Jun 11 '14 at 15:14
  • @AndroidDev Forgot to make the last two digits optional so it wasn't accepting two-digit years. Try now. – Niet the Dark Absol Jun 11 '14 at 15:30
  • I added two more examples at the end. Would you be so kind as to include those as well :-) – Johann Jun 11 '14 at 15:50
  • Now supports single-digit months. – Niet the Dark Absol Jun 11 '14 at 15:57
  • That's fantastic. Would it be too much to ask whether it's possible to limit days to a number between 1 and 31 and months between 1 and 12? @thg435 does raise a point about having invalid days and months. – Johann Jun 11 '14 at 16:05
  • @AndroidDev Sadly not. Well, possibly, but only if you're willing to write a monstrous regex to handle all possible cases, in particular which years are leap years ;) Use this regex to validate that your input is a date, then try to process that date and see if it makes sense. – Niet the Dark Absol Jun 11 '14 at 16:07
  • http://regex101.com/r/tH1cV7 shows a regex that handles leap years, although the format is different than these requested. It could be left up for an exercise for the user ;). – OnlineCop Jun 11 '14 at 16:15
1

Have you looked here? You don't want to use regex for stuff like this, since aforementioned 99.33.8888 isn't a date.

This clever function could solve your problem:

var isDate_ = function(input) {
    var status = false;
    if (!input || input.length <= 0) {
      status = false;
    } else {
      var result = new Date(input);
      if (result == 'Invalid Date') {
        status = false;
      } else {
        status = true;
      }
    }
    return status;
  }

Edit: I forgot you need to find something to validate. You could just run a simple regex like this: [0-9-/\.]{6,10}, which matches all of your examples

Community
  • 1
  • 1
LukasFT
  • 164
  • 2
  • 10