Javascript Data Type How to - Compare dates as format of '01/06/2013'








Question

We would like to know how to compare dates as format of '01/06/2013'.

Answer


<!DOCTYPE html>
<html>
<body>
<script>
function formatDate(strFecha) {<!--  ww  w . j a  v  a2  s  . c o m-->
  var df = strFecha.split("/");
        if (isNaN(df[0]) || isNaN(df[1]) || isNaN(df[2]) || (df[0] > 31) || (df[1] > 12)) {
            return false;
        }
        return new Date(df[2], df[1] - 1, df[0]);
}
document.writeln( formatDate("01/06/2013") > formatDate("31/05/2013"));
</script>
</body>
</html>

The code above is rendered as follows: