简体   繁体   English

验证时间戳的正则表达式

[英]Regular Expression to validate a timestamp

I need a regular expression to validate a timestamp of the format, using Javascript :我需要一个正则表达式来验证格式的时间戳,使用 Javascript

YYYY/MM/DD HH:MI:SS YYYY/MM/DD HH:MI:SS

I tried cooking up a few, but seems my regex skills fail to cover something or other.我尝试编写一些,但似乎我的正则表达式技能无法涵盖某些内容。

Please give me a reference or way to do it.请给我一个参考或方法。

PS : I mention regex, only as a suggestion. PS:我提到正则表达式,只是作为一个建议。 Im using Javascript and welcome any alternative.我使用 Javascript 并欢迎任何替代方案。

I would recommend to use Datejs for this.我建议为此使用Datejs Parsing the date yourself is not necessary and a Regex is not enough to validate a timestamp.不需要自己解析日期,正则表达式不足以验证时间戳。 With datejs you could parse the string in a date and you'll get null if its invalid:使用 datejs,您可以解析日期中的字符串,如果它无效,您将获得 null:

Date.parse("2009/06/29 13:30:10", "yyyy/MM/dd HH:mm:ss");

If you just want to validate the syntax, here is the POSIX regex:如果您只想验证语法,这里是 POSIX 正则表达式:

[0-9]{1,4}/[0-9]{1,2}/[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}

But if you want to check the semantics, I would process the string using your language of choice, there are too many cases you cannot cover with regular expressions (like leap years/seconds, daylight savings, etc)但是如果你想检查语义,我会使用你选择的语言处理字符串,有太多的情况你不能用正则表达式覆盖(比如闰年/秒、夏令时等)

You should consider not doing this with regular expressions, but rather just run the string through DateTime with the proper format string.您应该考虑不使用正则表达式执行此操作,而只需使用正确的格式字符串通过 DateTime 运行字符串。 That way you can ensure that it is indeed a valid timestamp and not just something that looks like it.这样你就可以确保它确实是一个有效的时间戳,而不仅仅是看起来像的东西。

Here is a regex I wrote earlier today for validating strings in a format similar to what you mentioned: YYYY-MM-DD hh:mm:ss .这是我今天早些时候编写的一个正则表达式,用于验证类似于您提到的格式的字符串: YYYY-MM-DD hh:mm:ss It does not identify some bad dates (for example, February 30th) but may be slightly better than using the simplistic \\d at each position.它不会识别一些错误的日期(例如,2 月 30 日),但可能比在每个位置使用简单化的\\d稍好一些。 Things to note:注意事项:

  1. you can specify just a date, just a time, or both date + time您可以只指定一个日期、一个时间,或同时指定日期 + 时间
  2. time can be in 12-hour or 24-hour format时间可以是 12 小时或 24 小时格式
  3. seconds are optional秒是可选的
  4. am/pm is optional上午/下午是可选的

    const std::string dateAndTimeRegex = "^\\\\s*" // ignore whitespace "(" // start of date "20[123][0-9]" // year: 2010, 2011, ..., through 2039 "\\\\W" // delimiter between year and month; typically will be "-" "([0]?[0-9]|1[012])" // month: 0 through 9, or 00 through 09, or 10 through 12 "\\\\W" // delimiter between month and day; typically will be "-" "([012]?[0-9]|3[01])" // day: 0 through 9, or 00 through 29, or 30, or 31 ")?" // end of optional date "\\\\s?" // optional whitespace "(" // start of time "([01]?[0-9]|2[0-3])" // hour: 0 through 9, or 00 through 19, or 20 through 23 "\\\\W" // delimiter between hours and minutes; typically will be ":" "([0-5][0-9])" // minute: 00 through 59 "(" // start of seconds (optional) "\\\\W" // delimiter between minutes and seconds; typically will be ":" "([0-5][0-9])" // seconds: 00 through 59 ")?" // end of optional seconds "(\\\\s*[AaPp][Mm])?" // optional AM, am, PM, pm ")?" // end of optional time "\\\\s*$"; // trailing whitespace

A comment from @kyrias hints that this regex will fail in a few months once we hit the year 2020. Depending on how you use it, you'll need to change "201[0-9]" to something else.来自@kyrias 的评论暗示,一旦我们到达 2020 年,这个正则表达式将在几个月内失败。根据您使用它的方式,您需要将“201[0-9]”更改为其他内容。

For example, if you are looking to validate the current date +/- a few years, you could change it to "20[12][0-9]".例如,如果您希望验证当前日期 +/- 几年,您可以将其更改为“20[12][0-9]”。 To validate 2000 through 2099, change it to "20[0-9]{2}".要验证 2000 到 2099,请将其更改为“20[0-9]{2}”。

I've changed the original regex above to look for 2010-2039.我已经更改了上面的原始正则表达式以查找 2010-2039。 Someone else can edit this answer in 20 years if necessary.如有必要,其他人可以在 20 年内编辑此答案。

I just wrote a Regex to control a MySQL datetime (and I think that it's not so different for Oracle or other database server).我刚刚写了一个 Regex 来控制 MySQL 日期时间(我认为对于 Oracle 或其他数据库服务器来说并没有太大的不同)。

This ReGex is very secure and controls the validity of the date (months in 28, 30 & 31 days, and even years with 29/02).这个 ReGex 非常安全并控制日期的有效性(28、30 和 31 天中的月份,甚至是 29/02 的年份)。 Then, it controls the time.然后,它控制时间。 The timestamp format is this one :时间戳格式是这样的:

YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS

Here is the Regex (quite long) :这是正则表达式(很长):

((((19|20)([2468][048]|[13579][26]|0[48])|2000)-02-29|((19|20)[0-9]{2}-(0[4678]|1[02])-(0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}-(0[1359]|11)-(0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}-02-(0[1-9]|1[0-9]|2[0-8])))\s([01][0-9]|2[0-3]):([012345][0-9]):([012345][0-9]))

Matches火柴

2013-04-05 17:59:59 | 2013-04-05 17:59:59 | 2013-07-30 01:22:42 | 2013-07-30 01:22:42 | 2099-12-30 23:59:59 | 2099-12-30 23:59:59 | 2016-02-28 00:00:00 2016-02-28 00:00:00

Non-Matches不匹配

2016-02-29 -01:01:02 | 2016-02-29-01:01:02 | 3000-04-24 17:42:21 | 3000-04-24 17:42:21 | 2012-03-03 24:24:02 | 2012-03-03 24:24:02 | 2012-03-03 21:60:45 2012-03-03 21:60:45

I don't think I can make a more secured one.我不认为我可以做一个更安全的。 Very functionnel and tested.非常功能和测试。 Please, if you use it, don't hesitate to give me feedback ;)请,如果您使用它,请不要犹豫给我反馈;)

FYI : If you just look for the date Regex control without time, please go there to find it : Regular Expression to match valid dates (check my post).仅供参考:如果您只是在没有时间的情况下查找日期 Regex 控件,请去那里找到它: 正则表达式以匹配有效日期(查看我的帖子)。

Cheers干杯

function validateTimestamp(timestamp) {

    if (!/\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}/.test(timestamp)) {
        return false;
    }

    var split = timestamp.split(/[^\d]+/);

    var year = parseFloat(split[0]);
    var month = parseFloat(split[1]);
    var day = parseFloat(split[2]);

    var hour = parseFloat(split[3]);
    var minute = parseFloat(split[4]);
    var second = parseFloat(split[5]);

    return hour < 25 && minute < 61 && second < 61 && month < 13 && day < 32;

}

For a valid date between year 1000 and year 2999 use :对于 1000 年和 2999 年之间的有效日期,请使用:

String pattern = "((1\\d{3})|(20\\d{2}))-((0\\d)|(1[0-2]))-(([1-2]\\d)|(3[0-1])) (([0-1]\\d)|(2[0-3])):([0-5]\\d):([0-5]\\d)";

The months can vary between 01 and 12, the days between 01 and 31 and the time between 00:00:00 and 23:59:59.月份可以在 01 到 12 之间变化,天可以在 01 到 31 之间变化,时间在 00:00:00 和 23:59:59 之间变化。

Here is how I build up a regex matching exactly SQL Timestamp yyyy-mm-dd hh:mm:ss in the range [ 1970-01-01 00:00:00 , 2037-12-31 23:59:59 ]以下是我如何在 [ 1970-01-01 00:00:00 , 2037-12-31 23:59:59 ] 范围内构建与 SQL 时间戳yyyy-mm-dd hh:mm:ss完全匹配的正则表达式

$leapYear = "(19[79][26]|198[048]|20[02][048]|20[13][26])";
$leapDate = "$leapYear-02-29";

$regularYear = "(19[789][0-9]|20[012][0-9]|203[0-7])";
$regularFebruaryDate = "02-([01][1-9]|2[0-8])";
$month31Date = "(0[13578]|10|12)-([012][1-9]|3[01])";
$month30Date = "(0[469]|11)-([012][1-9]|30)";
$regularDate = "$regularYear-($regularFebruaryDate|$month30Date|$month31Date)";

$hours = "(2[0-3]|[01][0-9])";
$minutes = "[0-5][0-9]";
$seconds = "[0-5][0-9]";

$sqlTimestamp = "($leapDate|$regularDate) $hours:$minutes:$seconds";

Note: I avoided the year 2038 to have one less edge-case.注意:我避开了 2038 年以减少一个边缘情况。 The max possible SQL timestamp is 2038-01-19 03:14:07 .最大可能的 SQL 时间戳是2038-01-19 03:14:07

perl 样式:[12]\\d{3}/[01]\\d/[0-3]\\d [0-2]\\d(?::[0-5]\\d){2}

Using使用

(\d\d)?\d\d\/\d\d?/\d\d? \d\d?:\d\d:\d\d

could validate the syntax, but as balpha points out, that doesn't make it a valid date.可以验证语法,但正如 balpha 指出的那样,这并不能使其成为有效日期。

function isTimestamp($input){
            var $regex = /^\d\d\d\d-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$/;
            return $regex.test($input);
        }

check please请检查

这是您正在寻找的正则表达式:

[0-9]{4}/(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]

Regular-Expressions.info是一个很好的关于 RegEx 的信息来源,它也有一个很好的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM