admin管理员组文章数量:1312748
I have this date format:
DAY.month.YEAR (today: 28.06.2011)
I will need a Regular Expression (RegEx) pattern for matching this date format.
Can anyone post a solution for this problem?
I have this date format:
DAY.month.YEAR (today: 28.06.2011)
I will need a Regular Expression (RegEx) pattern for matching this date format.
Can anyone post a solution for this problem?
Share Improve this question edited Jun 28, 2011 at 12:34 James 2,5663 gold badges29 silver badges34 bronze badges asked Jun 28, 2011 at 12:07 metaforcemetaforce 1,3775 gold badges17 silver badges26 bronze badges 4- 2 stackoverflow./questions/5032313/php-regex-date-validation – Igor Commented Jun 28, 2011 at 12:10
- 3 You really don't need a regex for this. To code the necessary logic into a regex (days in each month, leap years etc) would just be a mess. – David Hall Commented Jun 28, 2011 at 12:10
- 1 any particular flavour e.g. sed, perl, python, ruby, c#? Match plete string or just "28.06.2011"? Any limitations, i.e. is "01.01.1066" a valid date? – Fredrik Pihl Commented Jun 28, 2011 at 12:10
- yes, some limitations for ie, starting point from current year and so on, I'll use the expression in JavaScript... and it must not match only "28.06.2011", rather any date in that particular format ;) – metaforce Commented Jun 28, 2011 at 12:12
2 Answers
Reset to default 8Derived from http://www.regular-expressions.info/dates.html:
(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d
This matches a date in dd.mm.yyyy format from between 01.01.1900 and 31.12.2099. It will, however, still match invalid dates, because validating leap years, for example, can not be done with regex (at least not very easily).
However, a regex is probably unnecessary. Javascript example:
var date = "28.06.2011".split("."); // split up the date by the dots
// parse the ponents into integers
var day = parseInt(date[0]);
var month = parseInt(date[1]);
var year = parseInt(date[2]);
// if you want the date in a date object, which will fix leap years (e.g. 31.02 bees 03.03
var date = new Date(year, month - 1, day);
Note that when creating a date object, month
starts at zero.
Which method you use depends on what you need this for. If you want to find dates in a text, use the regex. If you simply want to parse the date into a date object, use the second method. Some extra validation is possibly necessary to make sure the date is valid, as the javascript Date
object does not care about February having 31 days, it simply wraps over to 3. of March.
If you wish to match the format of the date, than it will be enough to use:
\d\d\.\d\d\.\d\d\d\d
However, as David Hall pointed out in his ment to your question, you will still need to validate the date in your code. Doing this in a regex isn't easy, as you can see from Harpyon answer which - still making a "preliminary check that filter out many wrong possiblities" - also accepts 31.02.2011 as a valid date and misses out on the French revolution (14 July 1789).
本文标签: javascriptWhat regular expression should I use to match this date pattern DAYmonthYEARStack Overflow
版权声明:本文标题:javascript - What regular expression should I use to match this date pattern: DAY.month.YEAR? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741909931a2404382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论