formatting
Date.prototype.toUTCString
Returns the date as a string in the older RFC 7231 / IMF-fixdate format, e.g. 'Sun, 19 Jul 2026 00:00:00 GMT'. This is the exact format required by HTTP headers like Date, Expires, and cookies.
date.toUTCString() Parameters
| Parameter | Purpose |
|---|---|
| () | no arguments; returns fixed English UTC string |
| HTTP | matches the Date/Expires/Set-Cookie header format |
Examples
console.log(new Date('2026-07-19T00:00:00Z').toUTCString()); // 'Sun, 19 Jul 2026 00:00:00 GMT' HTTP-style
// Cookie expiry
document.cookie = 'k=v; expires=' + new Date(Date.now()+864e5).toUTCString(); cookie 1 day
console.log(new Date('2026-07-19T00:00:00Z').toISOString()); // '2026-07-19T00:00:00.000Z' - different format vs toISOString
Gotcha
toUTCString suffix is 'GMT' (RFC 7231), not 'Z' - do not use it as an API payload; use toISOString for that.
Related
Date.prototype.toISOString
Returns the date as an ISO 8601 extended-format string in UTC, always ending in 'Z'. This is the canonical machine-readable serialization and the format most APIs and databases accept.
Date.prototype.toString
Returns a fixed English representation of the date in LOCAL time, e.g. 'Sun Jul 19 2026 05:30:00 GMT+0530 (India Standard Time)'. Called implicitly by template literals and string concatenation.
Date.prototype.getUTCFullYear / getUTCMonth / getUTCDate / getUTCHours
The UTC-flavored getters mirror their local counterparts but read fields in Coordinated Universal Time, ignoring the host timezone. Use them whenever you need timezone-stable output such as logs, database keys, or ISO output.
Date.prototype.toJSON
Returns an ISO 8601 UTC string suitable for JSON serialization; called automatically by JSON.stringify when it encounters a Date. Equivalent to toISOString() for valid dates, but returns null for Invalid Dates instead of throwing.
Date.prototype.toLocaleDateString
Returns the date portion formatted for a given locale and options, without the time. For repeated formatting, cache an Intl.DateTimeFormat instead - it is significantly faster.