JavaScript toDateString() Method

Posted by

Introduction to the JavaScript toDateString() Method

The JavaScript toDateString() method is used to convert a Date object to a string, using the current user’s locale settings. This method is often used to convert a Date object to a string for displaying it to the user. The resulting string is in the format “Day Month Year” where the day is the weekday, the month is the month name, and the year is the year.

Syntax

The syntax for the JavaScript toDateString() method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

dateObj.toDateString()

[/dm_code_snippet]

Where dateObj is a Date object.

Example

Here is an example of the JavaScript toDateString() method:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
var dateString = date.toDateString();
console.log(dateString); // Outputs "Tue Jan 01 2019"

[/dm_code_snippet]

In this example, we create a Date object and store it in the date variable. We then call the toDateString() method on the Date object and store the resulting string in the dateString variable. We then log the string to the console, which outputs the string “Tue Jan 01 2019”.

Conclusion

The JavaScript toDateString() method is a useful method for converting a Date object to a string. This method is often used to display a Date object to the user in a readable format. The resulting string is in the format “Day Month Year” where the day is the weekday, the month is the month name, and the year is the year.