• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

JavaScript: Validate Correct Date/Time Format

Using JavaScript, I want to test if a date and time is in the allowed format.

These values are valid:
2011-9-9 22:3:00
2001-11-11 0:23:55
2091-1-1 2:00:11


These values are NOT valid:
11-9-9 22:3:00
2001-11-11
2091-1-1 2:00:11 xyz

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
</head>
<body>

<input type="text" id="timeDate" />
<input type="button" value="test" onclick="checkTimeDateFormat(document.getElementById('timeDate').value)" />

<script type="text/javascript">
/*<![CDATA[*/

function checkTimeDateFormat(str) {
 alert('The Format of the Date/Time field is correct.');
 alert('The Format of the Date/Time field is NOT correct.');
}

/*]]>*/
</script>

</body>
</html>

Open in new window

0
hankknight
Asked:
hankknight
  • 4
  • 2
3 Solutions
 
Gurvinder Pal SinghCommented:
pattern can be match by

var str = "2001-11-11 0:23:55";
var re = new RegExp("\d{4}\-\d{1,2}\-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}", "g");
var myArray = str.match(re);

if ( myArray != null)
{
   alert("correct date");
}
0
 
GewgalaCommented:
function checkTimeDateFormat(str) {
     var patt1 = "^\\d{4,4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{2,2}$";
     var reg = new RegExp(patt1);
     if (reg.test(str))
         alert('The Format of the Date/Time field is correct.');
     else
         alert('The Format of the Date/Time field is NOT correct.');
}

Open in new window

0
 
HonorGodCommented:
Don't forget that a user supplied "date" can match the pattern, and still be invalid.

For example:
  \d{4} simply verifies that the string contains exactly 4 digits, so everything from
            0000 to 9999 is "valid".

It is a good idea to convert the user specified input to a Date object (see http://www.w3schools.com/js/js_obj_date.asp for more details).

Given a Date object, you can format it (e.g., using information from this article http://www.experts-exchange.com/A_484.html) to a string, and then compare the user input string with the formatted date string.

Since a Date Object constructor will, by definition, result in a valid date, user input along the lines of:

2011-02-31 12:34:56

will be converted to

2011-03-03 12:34:56

and they will not match.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
GewgalaCommented:
Just for the record, the solution I posted does utilize the regex pattern of \d{4,4}, which means that the minimum amount of digits is 4 and the maximum is 4, so it has to exactly be 4.
0
 
GewgalaCommented:
Forgot to add in my above comment that I am talking about the year input.  But going off what hankknight posted of valid and invalid dates, my pattern is explained as follows:

^\\d{4,4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{2,2}$

Open in new window


\\d{4,4} means 4 (and only 4) digits

\\d{1,2} means 1 or 2 digits

\\d{2,2} means 2 (and only 2) digits

My original solution works great tested with the valid/invalid date time entries hankknight supplied, in that one simple JavaScript function.

0
 
HonorGodCommented:
Thanks for the assist, and the points.

Good luck & have a great day.
0
 
GewgalaCommented:
Glad I could help ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now