The JavaScript 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 accepts a single parameter:
- 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.
Example 1:
javascript
let date = "February 18, 2018 12:30 PM" ;
let msec = Date.parse(date);
console.log(msec);
|
Output:
1518937200000
Example 2: If the input string of date is not correct, it returns NaN i.e, not a number.
javascript
let date = "February 48, 2018 12:30 PM" ;
let msec = Date.parse(date);
console.log(msec);
|
Output:
NaN
Example 3: If the input string of the date is correct, it returns the difference between the date provided and January 1, 1970.
javascript
let date = "June 22, 2012" ;
let msec = Date.parse(date);
console.log(msec);
|
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 the JavaScript Date parse() method are listed below:
- Google Chrome 1 and above
- Internet Explorer 3 and above
- Mozilla Firefox 1 and above
- Opera 3 and above
- Safari 1 and above
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 May, 2023
Like Article
Save Article