Convert string into date using JavaScript
Last Updated :
15 May, 2024
In this article, we will convert a string into a date using JavaScript. A string must follow the pattern so that it can be converted into a date.
A string can be converted into a date in JavaScript through the following ways:
Method 1: Using JavaScript Date() constructor
Creating date object using date string: The date() constructor creates a date in human-understandable date form.
Example: In this example, we will convert a string into a date by creating a date object.
javascript
// It returns the Day,Month,Date,Year and time
// Using Date() constructor
let d = new Date("May 1,2019 11:20:00");
// Display output
console.log(d);
Output2019-05-01T11:20:00.000Z
Getting the string in DD-MM-YY format using suitable methods: We use certain methods such as:
- getDate-It Returns Day of the month(from 1-31)
- getMonth-It returns month number(from 0-11)
- getFullYear-It returns full year(in four digits )
Example: This example uses the approach to convert a string into a date.
JavaScript
// Using Date() constructor
let d = new Date("May 1, 2019 ");
// Display output
console.log(formatDate(d));
// Funciton to extract day, month, and year
function formatDate(date) {
let day = date.getDate();
if (day < 10) {
day = "0" + day;
}
let month = date.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
let year = date.getFullYear();
return day + "/" + month + "/" + year;
}
Method 2: Using JavaScript toDateString() method
This method returns the date portion of the Date object in human-readable form.
Example: This example shows the above-explained approach.
javascript
// Date object
let date = new Date(2019, 5, 3);
//Display output
console.log(date.toDateString());
Method 3: Using Date.parse() method
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);
Example: In this example, we will use date.parse() method to get time out of the string and convert it to get date output.
JavaScript
// Input string
let d = "May 1, 2019 "
// Using Date.parse method
let parse = Date.parse(d);
// Converting to date object
let date = new Date(parse);
// Display output
console.log(date);
Output2019-05-01T00:00:00.000Z
Using Intl.DateTimeFormat() and new Date(), the approach formats the input string using specified options for year, month, and day. It then creates a new Date object from the formatted string, effectively converting the string into a date.
Example: This example formats "May 1, 2019" into a date string, then converts it back to a Date object using Intl.DateTimeFormat() and new Date().
JavaScript
const dateString = "May 1, 2019";
const options = { year: 'numeric', month: 'long', day: 'numeric' };
// Format the input string
const formattedDate = new Intl.DateTimeFormat('en-US', options).format(
new Date(dateString));
// Create a new Date object from the formatted string
const date = new Date(formattedDate);
console.log(date);
Output2019-05-01T00:00:00.000Z
Supported Browsers
- Google Chrome
- Firefox
- Edge
- Opera
- Apple Safari
Similar Reads
How to Get Character of Specific Position using JavaScript ? Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1
4 min read
Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t
3 min read
Reverse a String in JavaScript We have given an input string and the task is to reverse the input string in JavaScript. Reverse a String in JavaScriptUsing split(), reverse() and join() MethodsThe split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characte
1 min read
JavaScript - Convert String to Title Case Converting a string to title case means capitalizing the first letter of each word while keeping the remaining letters in lowercase. Here are different ways to convert string to title case in JavaScript.1. Using for LoopJavaScript for loop is used to iterate over the arguments of the function, and t
4 min read
JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana',
3 min read
How to Convert String to Camel Case in JavaScript? We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin
4 min read
Extract a Number from a String using JavaScript We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console.Below are the methods to extract a number from string using JavaScript:Table of ContentUsing JavaScript match method with regExUsing
4 min read
JavaScript - Delete First Character of a String To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common onesUsing slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s
1 min read
JavaScript - How to Get Character Array from String? Here are the various methods to get character array from a string in JavaScript.1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. JavaScriptlet s = "Geeksf
2 min read
JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co
3 min read