Date.prototype.toDateString()

Returns just the date portion as an English string in the host time zone, dropping the time.

Signature
toDateString()
  • Added in JavaScript ES3 (1999)

Return value

stringA string representing the date portion of the given date, interpreted in the local time zone, with space-separated fields: the first three letters of the weekday name, the first three letters of the month name, the two-digit day of the month (zero-padded), and the four-or-more-digit year (zero-padded, possibly negative) — for example "Thu Jan 01 1970". Returns "Invalid Date" if the date is invalid.

Changelog

  • PHP ES2018The exact field widths and separators were standardized; earlier engines varied in day-of-month padding.

toDateString() gives you the calendar-day half of toString() and nothing else. It is fixed-width — always fifteen characters for years 0000–9999 — because the day is zero-padded: "Fri Jan 05 2024", not "Fri Jan 5 2024".

The genuinely useful case: same-day comparison

Two Date objects at different times on the same calendar day are never equal, and comparing their timestamps will not help. Comparing toDateString() results is a cheap, allocation-light way to ask “same local day?”:

a.toDateString() === b.toDateString()

This works because the string contains only year, month and day, all in local time. It is the local day, though — for a UTC-day comparison, slice toISOString() instead.

What it is not good for

The output is always English and always this exact shape, so it is not a display format for users outside an English-speaking audience — that is toLocaleDateString()'s job. And because it drops the offset, two machines in different time zones can render the same instant as different days, which makes the string unsafe to store or transmit.

Invalid dates yield the literal string "Invalid Date" rather than throwing.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Checking whether two timestamps fall on the same local calendar day
  • Grouping log entries by day for a quick console summary
  • Printing a date header in a developer-facing tool without pulling in Intl

Frequently asked questions

How do I check if two dates are on the same day?

Compare a.toDateString() === b.toDateString(), which ignores the time entirely. This compares local calendar days; for UTC days compare a.toISOString().slice(0, 10) instead.

How do I get a date string without the day of the week?

toDateString() always includes it, so slice it off with .slice(4) or use toLocaleDateString() with explicit year, month and day options and no weekday option.

Why is there a zero before single-digit days?

The specification requires the day of month to be two digits, zero-padded, so 5 January renders as "Fri Jan 05 2024". This keeps the string a fixed width for four-digit years.

More examples

Grouping timestamps by day

Using the date string as a Map key collapses every time on a given local day into one bucket, without any date-truncation arithmetic.

Example · javascript
const times = [
  new Date(2024, 4, 9, 8, 0),
  new Date(2024, 4, 9, 19, 30),
  new Date(2024, 4, 10, 1, 5),
];

const byDay = new Map();
for (const t of times) {
  const key = t.toDateString();
  byDay.set(key, (byDay.get(key) ?? 0) + 1);
}
console.log([...byDay]);

Fixed width makes slicing safe

Because every field is padded, positions 0–3, 4–7 and 11 onward always hold the weekday, month and year for four-digit years.

Example · javascript
const d = new Date(2024, 11, 25);
const s = d.toDateString();

console.log(s.length);
console.log(s.slice(0, 3));
console.log(s.slice(4, 7));
console.log(s.slice(11));

Local day, not UTC day

The ISO slice always shows the UTC day, while toDateString() shows the host's day; east-of-UTC hosts see them differ near midnight, and the final comparison shows whether an added hour crosses local midnight.

Example · javascript
const instant = new Date("2024-01-15T23:30:00Z");

console.log(instant.toISOString().slice(0, 10));
console.log(instant.toDateString());
console.log(
  instant.toDateString() ===
    new Date(instant.valueOf() + 60 * 60 * 1000).toDateString(),
);

Discussion

  • Be the first to comment on this lesson.