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.
It is a Date and time in the form of a string.
let dateObj = new Date(); let text = dateObj.toString(); console.log(text);
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.
- 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.
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);
Tue Mar 01 2022 00:00:00 GMT+0530 (India Standard Time)
const date = new Date(2022, 2, 19); let text = date.toString(); console.log(text);
Sat Mar 19 2022 00:00:00 GMT+0530 (India Standard Time)
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.