Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Momentjs parsing wildcard character

In momentjs is there a way to accept a wildcard while strictly parsing an input?

Strictly parse input by passing true as the third parameter.

I'd like to accomodate DD/MM/YYYY DD-MM-YYYY DD.MM.YYYY DD MM YYYY. I can add them all in an array, but I'd prefer to use * or . so I don't have to maintain the list. Is it possible to strictly require date format while ignoring delimiter?

moment('12/12/2012', ["DD/MM/YYYY", "DD-MM-YYYY", "DD.MM.YYYY", "DD MM YYYY"], true).isValid() === true;

// ideally ignore delimiter type, this evaluates to false
moment('12/12/2012', "DD*MM*YYYY", true).isValid() === false;
like image 948
Lex Avatar asked Sep 06 '25 16:09

Lex


1 Answers

Strict parsing is logically the opposite of wildcard. As an example, you probably want to reject '01/01-2020'.

That's specifically what the variant you're using is for (strict multi-format).

For non-strict cases the documentation is fairly clear:

The parser ignores non-alphanumeric characters, so both of the following will return the same thing.

moment("12-25-1995", "MM-DD-YYYY");
moment("12/25/1995", "MM-DD-YYYY");
like image 155
Amit Avatar answered Sep 09 '25 07:09

Amit