• JavaScript Video Tutorials

JavaScript Date toDateString() Method



The Date.toDateString() method is used in JavaScript to convert a date object into a string representation of the date portion only, without including the time information. The resulting string consists of day of the week, the month, the day of the month, and the year, but not the time and timezone information.

This method does not alter the original Date object; it only returns a string representation of its date component. If the provided Date object is invalid, this method returns "Invalid Date".

Syntax

Following is the syntax of JavaScript Date toDateString() method −

toDateString();

This method does not accept any parameters.

Return Value

This method returns a string representing the date portion of the Date object.

Example 1

In the following program, we are converting a date object to a string that represents only the date portion −

<html>
<body>
<script>
   const currentDate = new Date();
   const dateString = currentDate.toDateString();

   document.write(dateString);
</script>
</body>
</html>

Output

After executing, we can see a string that represents the date portion.

Example 2

Here, we are creating a date object with a specific date "2023-12-25" and using the toDateString() method to convert it into a string.

<html>
<body>
<script>
   const specificDate = new Date("2023-12-25");
   const dateString = specificDate.toDateString();

   document.write(dateString);
</script>
</body>
</html>

Output

The program returns "Sun Dec 31 2023" as output.

Example 3

If we create a date object with an invalid date, this method returns "invalid date" as result −

<html>
<body>
<script>
   const pastDate = new Date("2022-15-78");
   const dateString = pastDate.toDateString();

   document.write(dateString);
</script>
</body>
</html>

Output

The above program does not have month and day components within the range, this it returns "invalid date" as output.

Advertisements