The toDateString method in JavaScript is utilized to extract the date component from a Date object and presents it as a string.
Syntax
The syntax for the toDateString method is expressed as follows:
Example
dateObj.toDateString()
Return
A string that denotes the date segment of a Date object.
JavaScript Date toDateString method example
In this section, we will explore the toDateString method by examining several illustrative examples.
Example 1
Let’s examine an instance of retrieving the current date formatted as a string.
Example
<script>
var date=new Date();
document.writeln(date.toDateString());
</script>
Output:
Output
Tue Aug 07 2018
Example 2
Let’s examine an example that retrieves only the date component from a given Date object.
Example
<script>
var date=new Date("August 15, 1947 20:22:10 GMT+0530 (India Standard Time) ");
document.writeln(date.toDateString());
</script>
Output:
Output
Fri Aug 15 1947
Example 3
Let’s examine another example that retrieves only the date component from a given Date object.
Example
<script>
var date=new Date(1947, 7, 15, 20, 22, 10);
document.writeln(date.toDateString());
</script>
Output:
Output
Fri Aug 15 1947