JavaScript > Date

Parse() Method in JavaScript

How to parse a date string and return the number of milliseconds between mentioned dates in JavaScript?


By using parse() method we can parses a date string and return the number of milliseconds between the particular mentioned date from January 1st, 1970 (this is the date JavaScript treats as staring date).

EXAMPLE: Here we are returning the number of milliseconds from Jan 1st, 1970 to Oct 10th, 1992.

<p>Click the below button to return the number of milliseconds from between Jan 1, 1970 to Oct 10, 1992.</p>
<input type="button" value="Click" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var r = Date.parse("October 10, 1992");
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function myDate() in the<script>block which is connected to the Onclick of the HTML button. The document.getElementById("myId")returns the element that has Id"id=myID" in the HTML page. We need to parses a date string and return the number of milliseconds from jan 1st, 1970 to oct 10th, 1992, for that we are using Date.parse("October 10, 1992") method. Onclick of the button "Click" in the HTML code fires the function myDate() in the <script> block at the same time parse() method parses the date string and returns the number of milliseconds from 1st january of 1970 to the mentioned date in parse() method.

OUTPUT

 Views: 584 | Post Order: 116



Write for us