Compare Dates

Dates can easily be compared. The following example compares today's date with January 14, 2100:

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
var today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
    text = "Today is before January 14, 2100.";
} else {
    text = "Today is after January 14, 2100.";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>

JavaScript counts months from 0 to 11. January is 0. December is 11.

BackTable of ContentsNext