The Wayback Machine - https://web.archive.org/web/20240927054820/https://www.geeksforgeeks.org/javascript-date-parse-method/
Open In App

JavaScript Date parse() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The JavaScript Date parse() method parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970, UTC. If the argument is an invalid date string, it returns NaN (Not a Number).

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 milliseconds 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: Parsing Date String to Milliseconds

The code takes a date string “February 18, 2018, 12:30 PM” and parses it using the Date.parse() method, which returns the number of milliseconds since January 1, 1970. The result is logged to the console.

JavaScript
// Taking a date string as input.
let date = "February 18, 2018 12:30 PM";

// Calling parse function on input date string.
let msec = Date.parse(date);
console.log(msec);

Output
1518957000000

Example 2: Parsing Invalid Date String

The code attempts to parse an invalid date string “February 48, 2018 12:30 PM” using the Date.parse() method. Since the date is invalid, the method returns NaN (Not a Number), which is logged to the console.

JavaScript
// Taking wrong date string as input.
let date = "February 48, 2018 12:30 PM";

// calling parse function.
let msec = Date.parse(date);
console.log(msec);

Output
NaN

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: 

JavaScript Date parse() Method- FAQs

What does the Date.parse() method do in JavaScript?

The Date.parse() method parses a string representation of a date and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix Epoch).

What date formats are supported by Date.parse()?

Date.parse() supports ISO 8601 date formats (e.g., “YYYY-MM-DDTHH:mm.sssZ”). It may also support RFC 2822/822 formatted strings, depending on the JavaScript environment.

Can Date.parse() handle fractional seconds?

Yes, Date.parse() can handle fractional seconds in the ISO 8601 format (e.g., “YYYY-MM-DDTHH:mm.sssZ”).

What is the recommended format for Date.parse()?

The recommended format for Date.parse() is ISO 8601 (e.g., “YYYY-MM-DDTHH:mm.sssZ”) for compatibility and predictability.

What are the limitations of Date.parse()?

The limitations include:

  • Inconsistent parsing of non-ISO date strings across different browsers.
  • Limited handling of date strings that do not conform to ISO 8601 format.

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.


Previous Article
Next Article

Similar Reads

How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
Node.js Date.parse() API
The date-and-time.Date.parse() is a minimalist collection of functions for manipulating JS date and time module which is used to parse the date according to a certain pattern. Required Module: Install the module by npm or used it locally. By using npm.npm install date-and-time --saveBy using CDN link.<script src="/path/to/date-and-time.min.js"
2 min read
Moment.js Parse ASP.NET JSON Date
Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. We can use the moment() function passed with a string to parse dates in ASP.NET JSON format. The moment() function is used to create a moment using a string representing a date in ASP.NET JSON format. Syntax: moment( String ) Parameters: It takes a st
1 min read
Moment.js Parse Date Format Plugin
Parse Date Format is a moment.js plugin to can be used to extract a date/time string's format from a given string. This format can then be later used with other Moment methods. Write the below command in the terminal to install Date Format Plugin: npm install moment-parseformat The following are some of the functions in this plugin: parseFormat The
1 min read
How to Parse String to Date with MomentJS?
Moment.js is the date library in JavaScript that provides parsing, validation, manipulation, and formatting of dates and times. It has several methods to parse strings into date objects, accommodating various formats and time zones. Run the below command before running the code in your local system: npm i momentThese are the following approaches we
2 min read
JavaScript JSON parse() Method
The JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and returns a JavaScript object. Syntax: JSON.parse( string, function ) Parameters: This method accepts two parameters as mentioned above and described below: string: It is a required parameter and it contains a string that is written in JSON form
2 min read
How does the JSON.parse() method works in JavaScript ?
The JSON.parse() method in JavaScript is used to parse a JSON (JavaScript Object Notation) string and convert it into a JavaScript object. JSON is a lightweight data-interchange format that is easy for humans to read and write, and it is also easy for machines to parse and generate. Example: Here, JSON.parse() take a JSON-formatted string (jsonStri
1 min read
How to parse URL using JavaScript ?
Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method to create a HTML element, anchor tag and then use
2 min read
JavaScript SyntaxError - JSON.parse: bad parsing
This JavaScript exception thrown by JSON.parse() occurs if string passed as a parameter to the method is invalid. Message: SyntaxError: JSON.parse: unterminated string literal SyntaxError: JSON.parse: bad control character in string literal SyntaxError: JSON.parse: bad character in string literal SyntaxError: JSON.parse: bad Unicode escape SyntaxEr
2 min read
How to parse a URL into hostname and path in javascript ?
We can parse a URL(Uniform Resource Locator) to access its component and this can be achieved using predefined properties in JavaScript. For Example, the first example parses the URL of the current web page and the second example parses a predefined URL. Example 1:This example parses the URL of the current web page. C/C++ Code <body style="
1 min read
How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
How to parse HTTP Cookie header and return an object of all cookie name-value pairs in JavaScript ?
To parse HTTP Cookie header we will spilt the cookies data and create objects from the key-value extracted from cookies. Cookies are simply small text files that a web server sends to the user's browser. They contain the following data. Name-value pair with actual data.The expiry date for when the cookie becomes invalid.Domain and path of the serve
3 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read
How to Catch JSON Parse Error in JavaScript ?
JSON (JavaScript Object Notation) is a popular data interchange format used extensively in web development for transmitting data between a server and a client. When working with JSON data in JavaScript, it's common to parse JSON strings into JavaScript objects using the JSON.parse() method. However, parsing JSON data can sometimes lead to errors, e
1 min read
How to Parse XML in JavaScript?
Parsing XML data is important because it allows JavaScript applications to extract structured information from XML documents. We will explore two different approaches to Parse XML in JavaScript. Below are the approaches to parsing XML in JavaScript: Table of Content Using DOM ParserUsing xml2js LibraryUsing DOM ParserIn this approach, we are using
2 min read
How to Parse JSON in JavaScript ?
Parse JSON in JavaScript, accepting a JSON string as input and returning a corresponding JavaScript object with two methods, using JSON.parse() for parsing JSON strings directly and employing the fetch API to parse JSON responses from web APIs. These techniques are crucial for seamless data manipulation and retrieval in your JavaScript projects. Be
2 min read
How to Parse JSON Data in JavaScript ?
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write. It is easy for machines to parse and generate it. In JavaScript, parsing JSON data is a common task when dealing with APIs or exchanging data between the client and server. Table of Content Using JSON.parse() MethodUsing eval() Func
4 min read
How to Parse S-expressions in JavaScript?
The most simple and powerful notation used in Lisp programming is S-expressions and they are composed of atoms, say numbers or symbols or strings and lists (which too are S-expressions) parsing S-expressions in JavaScript can be done using a few basic techniques. What are S-expressions?To represent nested list data structures, we use s-expressions,
4 min read
How to Parse Float with Two Decimal Places in JavaScript?
JavaScript provides an inbuilt parseFloat() method to parse a string and returns a floating-point number. Below are the approaches to parse float to two decimal places in JavaScript: Table of Content Using parseFloat() MethodUsing Custom parseFloat Method Approach 1: Using parseFloat() MethodThe parseFloat() method converts a string to a floating-p
2 min read
How To Fetch And Parse RSS Feeds In JavaScript?
RSS (Really Simple Syndication) feeds are widely used for sharing updates from websites in a standardized XML format. By fetching and parsing RSS feeds, you can access the latest content from blogs, news sites, and other platforms. In this article, we’ll explore how to fetch and parse RSS feeds using JavaScript, using different methods. Table of Co
4 min read
How to check the input date is equal to today's date or not using JavaScript ?
Given a date object and the task is to determine whether the given date is the same as today's date or not with the help of JavaScript. There are two well-famous approaches are discussed here. Approach 1: Get the input date from user (var inpDate) and the today's date by new Date(). Now, use .setHours() method on both dates by passing parameters of
3 min read
How to convert UTC date time into local date time using JavaScript ?
Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. C/C++ Code <body> <h1 style="color:green;
1 min read
Difference Between new Date() and Date.now() in JavaScript
In JavaScript, handling dates and times is essential for various applications from displaying the current date to performing complex calculations involving time differences. Two commonly used methods for working with the dates are new Date() and Date.now(). Despite dealing with the time they serve different purposes and have distinct behaviors. The
3 min read
Node.js path.parse() Method
The path.parse() method is used to return an object whose properties represent the given path. This method returns the following properties: root (root name) dir (directory name) base (filename with extension) ext (only extension) name (only filename) The values of these properties may be different for every platform. It ignores the platform's trai
2 min read
Node.js querystring.parse() Method
The querystring.parse() method is used to parse a URL query string into an object that contains the key and pair values of the query URL. The object returned does not inherit prototypes from the JavaScript object, therefore usual Object methods will not work. During parsing, the UTF-8 encoding format is assumed unless there is an alternative charac
3 min read
Node.js date-and-time Date.isLeapYear() Method
The date-and-time.Date.isLeapYear() is a minimalist collection of functions for manipulating JS date and time module which is used to check if the given year is a leap year or not. Required Module: Install the module by npm or used it locally. By using npm: npm install date-and-time --save By using CDN link: <script src="/path/to/date-and-time.m
2 min read
Node.js date-and-time Date.transform() Method
The date-and-time.Date.transform() method is used to transform date and time from the existing string format to new string format. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script> Syntax: const transform(dat
2 min read
Node.js date-and-time Date.locale() Method
The date-and-time.Date.locale() method is used to get or switch the locale from one into another language. Required Module: Install the module by npm or used it locally. By using npm: npm install date-and-time --save By using CDN link: <script src="/path/to/date-and-time.min.js"></script> Syntax: locale([code 1="locale" language="[,"][/
1 min read
Node.js date-and-time Date.isSameDay() Method
The date-and-time.Date.isSameDay() method is used to check if the given dates are the same or not. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script> Syntax: isSameDay(date1, date2) Parameters: This method tak
2 min read
Node.js date-and-time Date.addSeconds() Method
The date-and-time.Date.addSeconds() method is used to add the extra Seconds to the existing date and time. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script>Syntax: const addSeconds(dateObj, seconds)Parameters
2 min read