Web developers often want most of the data and information in string data types. For example, displaying as text in HTML or the databases, storing the dates in the form of string data types is often more convenient. In this example, we will learn how to convert a date to a string data type.
How to Convert Date to String in JavaScript
To convert JavaScript date to string, use the toString() function. The toString() is a built-in JavaScript method that converts the date objects in the form of a string. The toString() function is used internally by JavaScript when an object must be displayed as a 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, then it will return the current date and time in the form of a string.
- If the parameter is passed in any other 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.
Output format
date
JavaScript code to convert date to string
let dateObj = new Date();
let text = dateObj.toString();
console.log(text);
Output
Sat Mar 19 2022 12:19:24 GMT+0530 (India Standard Time)
Explanation
- In the above code, we first defined a variable called “d” a user-defined variable using var.
- Then we initialized the JavaScript object by 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.
JavaScript new Date() with parameters
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.
Syntax
To specify only the year:
new Date(value);
To specify only the year and month:
new Date(year, indexOfMonth);
To specify only the year and month, day:
new Date(year, indexOfMonth, day);
To specify only the year and month, day, hours:
new Date(year, indexOfMonth, day, hours);
To specify only the year and month, day, hours, minutes:
new Date(year, indexOfMonth, day, hours, minutes);
To specify only the year and month, day, hours, minutes, seconds:
new Date(year, indexOfMonth, day, hours, minutes, seconds);
To specify only the year and month, day, hours, minutes, seconds, milliseconds:
new Date(year,indexOfMonth, day, hours, minutes, seconds, milliseconds);
Output
day month date year hour:minute: seconds UTC time
Example 1
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 2
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)
Example 3
const date = new Date(2022, 2, 19, 4, 5);
let text = date.toString();
console.log(text);
Output
Sat Mar 19 2022 04:05:00 GMT+0530 (India Standard Time)
Example 4
const date = new Date(2022, 2, 19, 4, 5, 6);
let text = date.toString();
console.log(text);
Output
Sat Mar 19 2022 04:05:06 GMT+0530 (India Standard Time)
Explanation
The output format of all the above will be the same. If all the parameters are not specified JavaScript will by default assume the initial values. For example, the hour: time: seconds is not mentioned JavaScript will assume the value as 00:00:00.
That’s it for this tutorial.
See also

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. Krunal has experience with various programming languages and technologies, including PHP, Python, and expert in JavaScript. He is comfortable working in front-end and back-end development.