views:

2489

answers:

8

Hi,

I need a regular expression to validate a timestamp of the format, using Javascript:

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.

P.S. : I mention regex, only as a suggestion. Im using Javascript and welcome any alternative.

A: 

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

Nikolai Ruhe
If you don't need leap seconds.
Nikolai Ruhe
+3  A: 

If you just want to validate the syntax, here is the POSIX regex:

[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)

soulmerge
i think it would be better to it as[0-9]{4,4}/[0-9]{2,2}/[0-9]{2,2} [0-9]{2,2}:[0-9]{2,2}:[0-9]{2,2}
Ratnesh Maurya
Depending on your needs, yes. But you can use {x} instead of {x,x}.
soulmerge
A: 

Regular-Expressions.info is a nice source of information about RegEx, it also has a good tutorial.

Zaagmans
A comment why this is downvoted would be deeply appreciated.I cannot see why giving a link to a RegEx reference website should be downvoted, especially when it states in the question "Please give me a reference or way to do it."
Zaagmans
A: 

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.

CoverosGene
+5  A: 

You should consider not doing this with regular expressions, but rather just run the string through DateTime with the proper format string. That way you can ensure that it is indeed a valid timestamp and not just something that looks like it.

Joey
+10  A: 

I would recommend to use Datejs for this. 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:

Date.parse("2009/06/29 13:30:10", "yyyy/MM/dd HH:mm:ss");
Tim Büthe
Thanks! It does what i need.
Mohit Nanda
+1  A: 
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;

}
J-P
A: 

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. It does not identify some bad dates (for example, February 30th) but may be slightly better than using the simplistic \d at each position. 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
  3. seconds are optional
  4. am/pm is optional

    const std::string dateAndTimeRegex =
        "^\\s*"                     // ignore whitespace
        "("                         // start of date
            "201[0-9]"              // year: 2010, 2011, ..., through 2019
            "\\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
    
Stéphane