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.

4
  • 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. Commented Jun 11, 2014 at 15:09
  • 1
    You cannot (and shouldn't) use regular expressions to validate dates. Is 99.33.8888 a valid date? Commented Jun 11, 2014 at 15:14
  • The last two examples don't match their signature. (they are MMddyyyy) Commented Jun 11, 2014 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)? Commented Jun 11, 2014 at 16:10

2 Answers 2

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)?)$/
Sign up to request clarification or add additional context in comments.

8 Comments

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.
@AndroidDev I broke the problem into two problems, then joined them together with the | - it is there ;)
@AndroidDev Forgot to make the last two digits optional so it wasn't accepting two-digit years. Try now.
I added two more examples at the end. Would you be so kind as to include those as well :-)
Now supports single-digit months.
|
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

2 Comments

The solution MUST allow for the formatting I specified.
just remember to validate the regex in some ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.