To convert a date to a string in JavaScript, you can use the “toString()” function. The toString() is a built-in method that converts the date objects into a string. JavaScript uses the function internally when an object must be displayed as text (like in HTML) or used as a string.
Syntax
Date.toString()
Parameters
By default, there is no necessary parameter that needs to be passed.
- If no parameter is passed, it will return the current date and time as a string.
- If the parameter is passed in any format other than string data types, the JavaScript will return the date in string format for that date entered by the user.
Return value
It is a Date and time in the form of a string.
Example 1
let dateObj = new Date();
let text = dateObj.toString();
console.log(text);
Output
Sat Mar 19 2022 12:19:24 GMT+0530 (India Standard Time)
- In the above code, we first defined a variable called “d” a user-defined variable using var.
- Then we initialized the JavaScript object using the new Date() method.
- We then defined another variable called “text”.
- We stored the date value in the form of a string and then stored the value in the “text” variable. Finally, we printed the output on the screen with the help of the console.log() method. The toString() method here converts the data types of the date into the String data types.
Example 2
The toString() function does not take any parameter, but we apply the toString() function to the date object, and the date object can be created using the new Date() function, and it can take multiple parameters.
const date = new Date(2022, 2);
let text = date.toString();
console.log(text);
Output
Tue Mar 01 2022 00:00:00 GMT+0530 (India Standard Time)
Example 3
const date = new Date(2022, 2, 19);
let text = date.toString();
console.log(text);
Output
Sat Mar 19 2022 00:00:00 GMT+0530 (India Standard Time)
That’s it.

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.