Hi I searched the web for checking date using javascript. I found the code example How to check whether a JavaScript date is valid?. The date is 2025-2-29 for testing. The code automatedly change to 2025-3-1 and show the date is valid. How can I stop it change the date? The result should be invalid for 2025-2-29
there is my code:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p id="demo"></p>
<p id = "validate"> </p>
<script>
const date = new Date("29Feb2025");
document.getElementById("demo").innerHTML = date;
isValid(date);
function isValid(date) {
alert(date);
if ( Object.prototype.toString.call(date) === "[object Date]") {
if ( !isNaN(date.getTime()) ) {
validate.innerHTML += date + " is valid. <br/> ";
} else {
validate.innerHTML += date + " is not valid. <br/> ";
}
} else {
validate.innerHTML += date + " is not valid. <br/> ";
}
}
</script>
</body>
</html>
The output:
JavaScript Dates
Using new Date()
Sat Mar 01 2025 00:00:00 GMT-0800 (Pacific Standard Time)
Sat Mar 01 2025 00:00:00 GMT-0800 (Pacific Standard Time) is valid.
Blockquote