Date.prototype.toString()

Returns the full date and time as an English string interpreted in the host time zone.

Signature
toString()
  • Added in JavaScript ES1 (1997)

Return value

stringA string representing the given date interpreted in the local time zone, in the form "Day Mon DD YYYY HH:MM:SS GMT±HHMM (Time Zone Name)", for example "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)". Returns "Invalid Date" if the date is invalid.

Errors

Throws a TypeError if called on a value that does not inherit from Date.prototype.

Changelog

  • PHP ES2018The format was standardized. Before this, engines disagreed on padding and on whether the parenthesized time zone name appeared at all, so older code that parsed this string may behave differently across engines.

toString() is the format you get for free: it is what appears when a Date lands in a template literal, in string concatenation, or in a console.log of a plain object. It joins toDateString() and toTimeString() with a space, interpreted in the host time zone.

It is for humans, and only English-speaking ones

The month and weekday names are always English regardless of the user's locale, and the offset is rendered as GMT+0200 rather than a name a reader would recognise. For anything a user will see, use toLocaleString(). For anything a machine will read, use toISOString(). toString() sits awkwardly between the two, which is why it is best treated as a debugging format.

The output moves with the machine

Two servers in different regions print different strings for the same instant. That makes toString() output useless as a cache key, a snapshot-test value, or anything compared across environments.

Coercion picks it, subtraction does not

` ${date}` and date + "" go through Symbol.toPrimitive with a default hint, which resolves to toString(). Arithmetic uses a number hint and reaches valueOf() instead — which is why date + date concatenates two long strings while date - date gives 0.

Example

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

When to use it

  • Dropping a date into a debug log or an error message
  • Reading a date's time zone offset while diagnosing an off-by-hours bug
  • Confirming what value a Date takes when interpolated into a template literal

Frequently asked questions

Why does my date print as a long English string with GMT in it?

That is Date.prototype.toString(), which JavaScript calls whenever a Date is coerced to a string. Use toISOString() for machine-readable output or toLocaleString() for user-facing output.

Does toString() use UTC or local time?

Local time — it renders the date in the host machine's time zone and appends that zone's offset and name. Use toUTCString() or toISOString() for a UTC rendering.

Why does date1 + date2 produce a giant string?

The + operator uses the default coercion hint, which for Date resolves to toString(), so both operands become strings and concatenate. Subtraction and unary + use the number hint and reach valueOf() instead.

More examples

The plus-operator trap

Adding to a Date produces a string because + prefers the default hint, which resolves to toString(). Subtraction forces the number hint, so it uses the timestamp. Prefix + to make the intent explicit.

Example · javascript
const d = new Date(Date.UTC(2024, 0, 1));

console.log(typeof (d + 1000));
console.log(typeof (d - 1000));
console.log((d - 1000) === d.valueOf() - 1000);

// Force the numeric hint explicitly
console.log(+d + 1000 === d.valueOf() + 1000);

Checking for an invalid date

toString() returns the literal text "Invalid Date" rather than throwing, but comparing against that string is fragile; testing valueOf() for NaN is the check that does not depend on formatting.

Example · javascript
const good = new Date("2024-02-29");
const bad = new Date("2024-02-30x");

console.log(good.toString() !== "Invalid Date");
console.log(bad.toString());

// The reliable check
console.log(Number.isNaN(bad.valueOf()));

Splitting date and time halves

toString() is literally toDateString() plus a space plus toTimeString(), so the date half is always a prefix of the full string.

Example · javascript
const d = new Date(2024, 5, 9, 17, 5, 0);
const full = d.toString();

console.log(d.toDateString());
console.log(full.startsWith(d.toDateString()));
console.log(full.includes("GMT"));

Discussion

  • Be the first to comment on this lesson.