JavaScript | Date.parse()
The Date.parse() function is an inbuilt function in JavaScript which helps us 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 function takes a parameter of datestring which is taken as the input.
Return Values: It returns an integer value representing the number of 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.
Code #1:
<script> // Taking a date string as input. var date = "February 18, 2018 12:30 PM"; // Calling parse function on input date string. var msec = Date.parse(date); document.write(msec); </script> |
Output:
1518937200000
Code #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 = "February 48, 2018 12:30 PM"; // calling parse function. var msec = Date.parse(date); document.write(msec); </script> |
Output:
NaN
Why 01 Jan 1970 00:00:00 GMT?
It is called the Unix time i.e. the time Unix was invented. Almost all the programming languages, operating systems use this as their reference starting time.
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.
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- this in JavaScript
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.



