
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the use of parsing dates in JavaScript?
The date. parse() method parsing a string representation of a date, and returns the number of milliseconds.
parse() parse a date string and returns the time difference between midnight 1, 1970 to provided date.
The date. parse() method is used to know the exact number of milliseconds that have passed from midnight, January 1, 1970, till the date we provide.
Syntax
Following is the syntax of the parse() function −
Date.parse(dateString)
This method accepts only one parameter and holds the date as a string. It returns an integer value that represents the number of milliseconds between January 1, 1970, and the date provided. The machine can’t recognize the string or the input string is invalid. It will return not a 'NaN' instead of an integer.
Example: 1
If the input of the date is not correct, then it will return 'NaN' i.e, not a number.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>parsing date</title> </head> <body> <script> var date = "February 32, 2018 12:30 PM"; var msec = Date.parse(date); document.write(msec); </script> </body> </html>
Example 2
In the following example, we are calculating the number of years b/w January 1, 1970 and provided date.
<!DOCTYPE html> <html> <head> </head> <body> <p>Calculate the number of years between 19th July 2022 and January 1, 1970:</p> <p id="demo"></p> <script> const minute = 1000 * 60; const hour = minute * 60; const day = hour * 24; const year = day * 365; const date = Date.parse("July 19, 2022"); let years = Math.round(date / year); document.getElementById("demo").innerHTML = years; </script> </body> </html>
Example 3
In the following example, we are calculating the number of milliseconds when we were parsing the date.
<!DOCTYPE html> <!DOCTYPE html> <html lang="en"> <head> <title>parsing date</title> </head> <body> <script> var date = "July 19, 2022"; var msec = Date.parse(date); document.write(msec); </script> </body> </html>
Example 4
In the following example, we are getting the present date with the help of a new Date() method and after that, we were parsing the date and finding the millisecond.
<!DOCTYPE html> <!DOCTYPE html> <html lang="en"> <head> <title>parsing date</title> </head> <body> <script> var date = new Date(); document.write(" The present date is: " + date + "</br>"); var msec = Date.parse(date); document.write(msec); </script> </body> </html>
- Related Articles
- What is the use of Atomics in JavaScript?
- What is the use of OBJECT.assign() in javascript?
- What is the use of Map in JavaScript?
- What is the use of window.location in javascript?
- What is the use of sentry in javascript?
- What is the use of JavaScript cookies?
- What is the use of declaring variables in JavaScript?
- What is the use of weakSet.has() method in JavaScript?
- What is the use of Object.is() method in JavaScript?
- What is the use of proxy() object in JavaScript?
- What is the use of Atomics.store() method in JavaScript?
- What is the use of Object.isFrozen() method in JavaScript?
- What is the use of apply() function in JavaScript?
- What is the use of Math.clz32() method in JavaScript?
- What is the use of Math.imul( ) Function in JavaScript?
