
- 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
Converting a string to a date in JavaScript
In this article, we are going to discuss how to convert a string value to a Date object in JavaScript. There are two ways to achieve this −
Using the constructor of the Date class − This constructor accepts a string value representing the date value, converts it into a Date object, and returns the result.
Using the Date.parse() method − Same as the Date constructor this method accepts a string value parses and returns the date value in the form of milliseconds.
Let us see these solutions with examples −
Using the Date() constructor
The most commonly used way to convert a string into a date object is using the constructor of the Date() class. Following is the syntax of the Date constructor −
var date = new Date('date_string');
This accepts arguments in various forms.
The new Date() returns the current system date and time, including the time zone information of your local system. If you are not passing the parameter in the new Date() function.
Passing date object as the argument. The new Date() will return a date object as rendered by the date string passed as an argument.
When we pass a date in string format to the new Date(), it is converted into a date object. And the string format needs to be yyyy−mm−dd, for it to work. Other date formats may not be converted to a date object by this method.
Example
In the example, we are checking which date form is valid in the Date class’s constructor.
<!DOCTYPE html> <html lang="en"> <head> <title></title> </head> <body> <script> var date1 = new Date("2022-08-04"); // here we are taking yyyy-mm-dd format var date2 = new Date("2022/08/03"); // here we are taking yyyy/mm/dd format var date3 = new Date("2022-15-03"); // here we are taking yyyy/dd/mm format var date4 = new Date("15-02-2022"); // here we are tacking dd/mm/yyyy format document.write(date1 + "<br>"); document.write(date2 + "<br>"); document.write(date3 + "<br>"); document.write(date4 + "<br>"); </script> </body> </html>
Note − The new Date() can be used to convert the string into a date only if it qualifies the ISO 8601 format yyyy−mm−dd hh−mm−ss
Using the date.parse() method
Date.Parse() is an alternate solution to convert the string date. It returns a numeric value but not a date object. So, it will require further processing if you expect a date object.
This method converts the given date to a numerical value representing the milliseconds. The return value is the number of milliseconds since 1−Jan−1970 midnight time. It is the same as the time stamp format with the difference that instead of seconds, Date.parse() returns a millisecond value.
Example
In the following example, we are converting string date to date format. But here the return value is in milliseconds.
<!DOCTYPE html> <html lang="en"> <body> <p>Date.Parse Method</p> <script> var date1 = Date.parse("2022-08-04"); // here we are taking yyyy-mm-dd format var date2 = Date.parse("2022/08/03"); // here we are taking yyyy/mm/dd format var date3 = Date.parse("2022-15-03"); // here we are taking yyyy/dd/mm format var date4 = Date.parse("15-02-2022"); // here we are tacking dd/mm/yyyy format document.write(date1 + "<br>"); document.write(date2 + "<br>"); document.write(date3 + "<br>"); document.write(date4 + "<br>"); </script> </body> </html>
Note −
The Date. parse() is the same as the new Date() except for the return type, which makes it the best fit for checking if a date value is of the correct format and can also be used to allocate a date by using the dateObj.setTime(Date. parse(DateString)).
The Date.Parse() returns milliseconds that can be used for precision comparison of dates even without converting them to actual date objects with new Date().
Example
In the following example, we can see that the Date.parse() is the same as the new Date() when it comes to the type of input value it can accept but if the date is of a valid format.
Especially when we are dealing with an API response value. In such cases, we may need to perform a check to ensure the value returned by the backend confirms the date format accepted by the Date.parse() or even the new Date() function. Just an isNaN() check can help identify and safely land into the date conversion method.
<!DOCTYPE html> <html lang="en"> <body> <p>Date.Parse Method and new Date() Method</p> <script> let stringsFromAPI = ["2022-08-04", "20-11-2022"]; stringsFromAPI.forEach((d) => { if (!isNaN(Date.parse(d))) { console.log(new Date(d)); } }); </script> </body> </html>
- Related Articles
- Converting string to a binary string - JavaScript
- Converting string to date in MongoDB?
- Converting a date in MySQL from string field?
- Converting a string to NATO phonetic alphabets in JavaScript
- Converting string to MORSE code in JavaScript
- Converting string to an array in JavaScript
- Converting whitespace string to url in JavaScript
- JavaScript: Converting a CSV string file to a 2D array of objects
- Converting multi-dimensional array to string in JavaScript
- Converting array to phone number string in JavaScript
- Converting a StringBuilder to String in Java
- How to convert a JavaScript date object to a string?
- How to convert a string in to date object in JavaScript?
- Converting Odd and Even-indexed characters in a string to uppercase/lowercase in JavaScript?
- Converting a comma separated string to separate arrays within an object JavaScript
