Created
January 5, 2013 11:25
-
-
Save mplungjan/4461086 to your computer and use it in GitHub Desktop.
Test a date for validity by creating JS date object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> | |
<title>Test a date string - by mplungjan</title> | |
<script> | |
function isValidDateCheck(dString) { | |
var dRe = /^(\d{1,2})([\-\/])(\d{1,2})\2(\d{4}|\d{2})$/ | |
if (!dRe.exec(dString)) { | |
console.log(dString, " was not correct format"); | |
return false; | |
} | |
dString.replace(/-/g,"/"); // make sure it parses as date - replace this part if you do not allow dashes | |
var date = new Date(dString); // create a date object | |
console.log(dString,date); | |
if (!isNaN(date)) { // it may give NaN - if not test the parts | |
var parts = dString.split("/"); // split on slash | |
var dd = parseInt(parts[1],10); // day number | |
var mm = parseInt(parts[0],10)-1; // month - JS months start at 0 | |
var yyyy = parseInt(parts[2],10); // year | |
// return true if all parts match | |
return dd===date.getDate() && mm === date.getMonth() && yyyy===date.getFullYear(); | |
} | |
// here the date was not parsed as a date | |
console.log(dString,"gave isNaN") | |
return false; | |
} | |
var tests = [ | |
{d:"12/33/2012",a:false}, | |
{d:"12/12/2012",a:true}, | |
{d:"02/29/2012",a:true}, | |
{d:"02/29/2013",a:false}, | |
{d:"01/01/2013A",a:false}, | |
{d:"01/ 01 /2013",a:false} | |
]; | |
window.onload=function() { doTest(tests)}; | |
// ---------------- ignore the rest of the page -------------- | |
// test function - | |
function doTest(arr) { | |
for (var i=0;i<arr.length;i++) { | |
var d=arr[i].d; | |
var a=arr[i].a; | |
var html="<br/><span class='pre "+(a?"passed":"failed")+"'>"+d+"</span>: "+ | |
(a===isValidDateCheck(d)?"<span class='passed'>("+a+")</span>":"<span class='failed'>failed</span>"); | |
document.getElementById("output").innerHTML+=html; | |
} | |
} | |
if (!window.console) window.console={ log:function(str) {alert(str)}}; | |
</script> | |
<style type='text/css'> | |
#main { padding:3px } | |
.passed { color:green } | |
.failed { color:red } | |
.pre { font-family:monospace } | |
</style> | |
</head> | |
<body> | |
<div id="main"> | |
<p>This will handle actual dates and give you the chance to find what part of the date was invalid - using the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date">DATE OBJECT</a></p> | |
<p><b>NOTE:</b> several browsers will happily parse what seems to be an invalid date and make a date object out of it. <br/>For example 02/29/2013 will parse as 1st of March 2013, hence my test to see if the parts entered made sense when used in an actual date.</p> | |
<div id="output"></div> | |
<hr/> | |
<p><b>Tested in</b> | |
<ul> | |
<li>Win7: | |
<ul> | |
<li>Chrome 23 (only one to give isNaN on the first date)</li> | |
<li>IE 9</li> | |
</ul> | |
</li> | |
<li>Win XP: | |
<ul> | |
<li>FX 17</li> | |
<li>IE 8</li> | |
<li>Safari 5</li> | |
<li>Opera 11 and 12</li> | |
</ul> | |
</li> | |
</ul> | |
</p> | |
</div> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment