Javascript Data Type How to - Parse Date string in `dd-M-yyyy` format








Question

We would like to know how to parse Date string in `dd-M-yyyy` format.

Answer


<!DOCTYPE html>
<html>
<body>
  <input type="text" id="date" />
  <input type="submit" id="submit" />
  <p id="result"></p>
<!--  w  ww .j  a  va2s . c  o  m-->
<script type='text/javascript'>

document.getElementById("submit").onclick = function() {
    var date = Date.parse(document.getElementById("date").value);
    document.getElementById("result").innerHTML = new Date(date);
};

</script>

</body>
</html>

The code above is rendered as follows: