toISOString() serializes a Date as a portable UTC ISO 8601 string—the standard format for APIs, databases, and logs. This guide covers syntax, the Z suffix, date-only extraction, comparisons, and error handling.
01
Syntax
toISOString()
02
UTC
Always Z
03
ISO 8601
Standard
04
slice(0,10)
Date part
05
APIs
JSON payloads
06
Invalid
RangeError
Fundamentals
Introduction
When you store or send dates across systems, you need a format that sorts chronologically and parses reliably. toISOString() converts any valid Date instant into UTC text that pairs naturally with Date.parse() and JSON APIs.
This is the UTC calendar date of the instant—not necessarily your local calendar date near midnight boundaries.
Example 5 — Guard Before toISOString() (Invalid Date)
Invalid dates throw RangeError—validate with getTime() first.
JavaScript
const bad = new Date("not-a-date");
if (Number.isNaN(bad.getTime())) {
console.log("Cannot format invalid date");
} else {
console.log(bad.toISOString());
}
Without the guard, bad.toISOString() throws RangeError: Invalid time value and can crash request handlers.
Applications
🚀 Common Use Cases
REST APIs — send scheduledAt fields as ISO strings.
Database columns — store timestamps in portable text or derive from ISO.
Structured logs — prefix lines with new Date().toISOString().
Unit tests — assert expected ISO output after UTC setters.
CSV / exports — use .slice(0, 10) for UTC date columns.
Round-trip parsing — pair with Date.parse() for read/write workflows.
🧠 How toISOString() Builds the UTC String
1
Read instant
Engine loads the Date’s epoch milliseconds.
getTime
2
Convert to UTC
Calendar and clock fields are computed in UTC.
UTC
3
Format ISO 8601
Zero-padded date, time, ms, and trailing Z are assembled.
ISO
4
🌐
Return string
A new string is returned; the Date object is unchanged.
Important
📝 Notes
toISOString() always outputs UTC with a Z suffix.
Read-only: does not mutate the Date.
Invalid dates throw RangeError—unlike toDateString() which returns "Invalid Date".
.slice(0, 10) gives UTC date, not local date.
Lexicographic sort of ISO strings equals chronological sort.
JSON.stringify on Date objects uses ISO format via toJSON().
Compatibility
Browser & Runtime Support
Date.prototype.toISOString() has been available since ES5. It works in every modern browser and Node.js.
✓ Baseline · ES5
Date.prototype.toISOString()
Supported in Chrome, Firefox, Safari, Edge, Internet Explorer 9+, and all Node.js versions. No polyfill required.
99%Universal API
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Date.toISOString()Excellent
Bottom line: Safe everywhere. Remember UTC output, RangeError on invalid dates, and slice for date-only.
Wrap Up
Conclusion
toISOString() is the standard way to serialize JavaScript Date values as portable UTC ISO 8601 strings. Use it for APIs, logs, and storage; use toDateString() for quick local date labels.
Next, learn toJSON() for how JSON.stringify serializes dates, or review Date.parse() for the read side of ISO strings.
Use ISO strings in API request and response bodies
Validate with Number.isNaN(date.getTime()) before formatting
Use .slice(0, 10) when you need UTC YYYY-MM-DD
Pair with Date.parse() for round-trip workflows
Prefer ISO over locale strings for machine-readable storage
❌ Don’t
Assume ISO output reflects local calendar date near midnight
Use toLocaleString() when you need ISO (different format)
Call toISOString() on invalid dates without a guard
Parse ISO strings with brittle regex when Date.parse works
Display raw ISO to end users without locale formatting
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Date.toISOString()
Your foundation for UTC ISO serialization in JavaScript.
5
Core concepts
📝01
Syntax
toISOString()
API
🌐02
UTC + Z
Always.
Timezone
📋03
ISO 8601
Standard.
Format
✅04
APIs
JSON ready.
Use case
⚠05
Invalid
RangeError.
Guard
❓ Frequently Asked Questions
A UTC string in ISO 8601 format: YYYY-MM-DDTHH:mm:ss.sssZ (for example, "2026-03-15T12:00:00.000Z"). The trailing Z means UTC.
UTC always. The output reflects the same instant expressed in Coordinated Universal Time, regardless of your local timezone.
toISOString() returns a full UTC instant with time and Z suffix. toDateString() returns a local calendar date string without time.
Use date.toISOString().slice(0, 10). This gives the UTC calendar date portion of the instant.
It throws a RangeError with message "Invalid time value". Validate with Number.isNaN(date.getTime()) first.
No. It is a read-only formatter—it returns a new string and leaves the Date unchanged.
Did you know?
ISO strings sort alphabetically in the same order as chronological time—handy for filename prefixes like 2026-03-15T12-00-00Z-backup.json (replace colons if your filesystem requires it).