Javascript Data Type How to - Compare two timestamps








Question

We would like to know how to compare two timestamps.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--  ww  w  .  ja  v  a2  s .  c  om-->
    d1 = new Date("2014-01-15 00:00:00.0");
    d2 = new Date("2014-01-16 00:00:00.0");
    if ((d1 - d2) == 0) {
        document.writeln("equal");
    } else if (d1 > d2) {
        document.write("d1 greater d2");
    } else {
        document.write("d1 less than d2");
    }
}
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: