JavaScript date.toDateString() method
The date.toDateString()
method in JavaScript returns the date portion of a Date
object as a human-readable string, without the time.
Syntax:
Returns:
- A string representing the date in the format:
Weekday Month Day Year
(e.g., "Tue Oct 22 2024").
Example 1: Getting the Date String of the Current Date
Output:
Explanation:
- The method extracts and formats the date part (day, month, year) from the
Date
object. In this case, it formats the current date.
Example 2: Getting the Date String for a Specific Date
Output:
Explanation:
- The date string
Sun Dec 17 1995
is generated from theDate
object that represents December 17, 1995.
Example 3: Date Object with Modified Time, toDateString
Ignores Time
Output:
Explanation:
- Although the
Date
object includes both date and time,toDateString()
only returns the date portion (ignoring the time: 20:18:00).
Summary:
date.toDateString()
is useful when you want a clean, human-readable string of just the date without any time information.- The output format is standardized as
Weekday Month Day Year
.