Below is the example of Date parse() method.
- Example:
<script>// Taking a date string as input.vardate ="February 18, 2018 12:30 PM";// Calling parse function on input date string.varmsec = Date.parse(date);document.write(msec);</script>chevron_rightfilter_none - Output:
1518937200000
The date.parse() method is used to know the exact number of milliseconds that have passed since midnight, January 1, 1970, till the date we provide.
Syntax:
Date.parse(datestring);
Parameters: This method accept a single parameter as metnioned above and described below:
- datestring: This parameter holds the date as a string.
Return Values: It returns an integer value representing the number of a millisecond between midnight January 1, 1970, and the date provided. If by any means, the machine can’t recognize the string or the input string is invalid, it will return “NaN” instead of an integer.
More codes for the above method are as follows:
Program 1: If the input string of date is not correct, it return NaN i.e, not a number.
<script> // Taking wrong date string as input. var date = "February 48, 2018 12:30 PM"; // calling parse function. var msec = Date.parse(date); document.write(msec); </script> |
Output:
NaN
Program 2: If the input string of date is not correct, it return NaN i.e, not a number.
<script> // Taking wrong date string as input. var date = "June 22, 2012"; // Calling parse function. var msec = Date.parse(date); document.write(msec); </script> |
Output:
1340303400000
Note: Once we get the millisecond count between two dates, we can easily find the number of hours, days, months, years, etc by simple maths calculation.
Supported Browsers: The browsers supported by JavaScript Date parse() method are listed below:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Opera
- Safari
Recommended Posts:
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- JavaScript JSON parse() Method
- How to check the input date is equal to today's date or not using JavaScript ?
- How to convert UTC date time into local date time using JavaScript ?
- How to parse URL using JavaScript ?
- JavaScript SyntaxError - JSON.parse: bad parsing
- Node.js | path.parse() Method
- Node.js | querystring.parse() Method
- Why is Parse Server the future of Backend As A Service?
- Python | Parse a website with regex and urllib
- Node | url.parse(urlString, parseQueryString, slashesDenoteHost) API
- How to universally parse JSON into blocks using jQuery ?
- How to parse and process HTML/XML using PHP ?
- JavaScript Date toLocaleTimeString() Method
- JavaScript Date UTC() Method
- JavaScript Date getHours() Method
- JavaScript Date getMinutes() Method
- JavaScript Date getMonth() Method
- JavaScript Date getSeconds() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


