ISO Formatted Date with optional time. Test with sample data Normal Quoted. Sample Matches. Group 0 T Group 1 - Group 2 :. Group 0 Group 0 Group 1 -. Group 0 Group 1. Group 0 W Group 1 -. The hyphens are optional. Ordinal date e. The hyphen is optional:. Week of the year e. Week date e. Hours and minutes e. The colon is optional:. Hours, minutes, and seconds e. The colons are optional:. Time zone designator e. The colons and the minutes are optional:. Hours, minutes, and seconds with time zone designator e.
All the colons are optional. The minutes in the time zone designator are also optional:. Calendar date with hours, minutes, and seconds e. A space is required between the date and the time. The hyphens and colons are optional.
This regex matches dates and times that specify some hyphens or colons but omit others. This does not follow ISO A more complicated solution is needed if we want to match date and time values that specify either all of the hyphens and colons, or none of them.
The cleanest solution is to use conditionals. But only some flavors support conditionals. If conditionals are not available, then we have to use alternation to spell out the alternatives with and without delimiters.
This regex is also incomplete since the specification states that you cannot mix the extended and basic formats see ISO , section 4. The validation we use on Learning Locker is on Github, I'm sure it's not complete but it works for us and passes our conformance tests. Only issue with that fiddle is that it validates 30th and 31st of February which is obvious non-sense and will validate 29th of February regardless of leap years. Show 14 more comments. SnailCrusher SnailCrusher 1, 10 10 silver badges 9 9 bronze badges.
Add a comment. Don't quote the regex when specifying a regex in js. Forward slash is enough. Peter Kuhar Peter Kuhar 4 4 silver badges 6 6 bronze badges. Thank you Peter. For one shouldn't match the standard.
Below is my version: it is stricter than the ISO as it forces to have date, time hh:mm:ss and time zone. The only optional part is millisecond.
JavaScript date. Here's a demo snippet if you have to pass to mongo and need to convert. King Friday King Friday These are specified by a leading minus sign — and can also include more year characters for example a 5-digit year formatted as YYYYY if agreed upon previously by both the sender and receiver.
If agreed upon by both the sender and receiver, we can modify the year representation to include additional year characters. The regular expressions developed above have some degree of intelligence in that they only accept months between 01 and 12 and days between 01 and We can modify our original regular expression to only include valid dates as seen below.
However, this uses a lot of OR symbols and nested groups and easily become confusing to any programmer.
In addition, this does not rule out invalid leap days. For example, it will match although is not a leap year. To do this check will require checking if the year is divisible by 4.
0コメント