Date parse() Method - Javascript Date

Javascript examples for Date:parse

Description

The parse() method parses a date string.

Parameter Values

Parameter Description
datestring Required. A string representing a date

Return Value:

A Number, representing the number of milliseconds between the specified date-time and midnight January 1, 1970

The following code shows how to Calculate the number of years between January 1, 1970 to March 21, 2012:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from w  w w.j a v a 2  s.com
    var d = Date.parse("March 21, 2012");
    var minutes = 1000 * 60;
    var hours = minutes * 60;
    var days = hours * 24;
    var years = days * 365;
    var y = Math.round(d / years);

    document.getElementById("demo").innerHTML = y;
}
</script>

</body>
</html>

Related Tutorials