JavaScript Date toDateString()
Syntax & Examples
toDateString() method
The toDateString() method of the Date object in JavaScript returns the date portion of the Date as a human-readable string. This string includes the day of the week, month, day of the month, and year.
Syntax of toDateString()
The syntax of Date.toDateString() method is:
toDateString()
This toDateString() method of Date returns the "date" portion of the Date as a human-readable string like 'Thu Apr 12 2018'.
Return Type
Date.toDateString() returns value of type String
.
✐ Examples
1 Using toDateString() to get the date portion of a Date object
In JavaScript, we can use the toDateString()
method to get the date portion of a Date object as a human-readable string.
For example,
- We create a new Date object
date
representing January 1, 2000. - We use the
toDateString()
method to get the date portion of thedate
object. - The date string is stored in the variable
dateString
. - We log
dateString
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('2000-01-01');
const dateString = date.toDateString();
console.log(dateString);
Output
Sat Jan 01 2000
2 Using toDateString() with a specific date
We can use the toDateString()
method to get the date portion of a specific Date object.
For example,
- We create a new Date object
date
representing July 4, 1776. - We use the
toDateString()
method to get the date portion of thedate
object. - The date string is stored in the variable
dateString
. - We log
dateString
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('1776-07-04');
const dateString = date.toDateString();
console.log(dateString);
Output
Thu Jul 04 1776
3 Using toDateString() with the current date
We can use the toDateString()
method to get the date portion of the current date.
For example,
- We create a new Date object
date
representing the current date and time. - We use the
toDateString()
method to get the date portion of thedate
object. - The date string is stored in the variable
dateString
. - We log
dateString
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date();
const dateString = date.toDateString();
console.log(dateString);
Output
Depends on the current date (e.g., 'Mon May 27 2024')
Summary
In this JavaScript tutorial, we learned about toDateString() method of Date: the syntax and few working examples with output and detailed explanation for each example.