JavaScript Date toISOString() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
UTC ISO 8601

What You’ll Learn

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

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.

For human-readable local labels without time precision needs, use toDateString(). For locale-aware display, use toLocaleDateString().

Understanding the toISOString() Method

Date.prototype.toISOString() returns a string like 2026-03-15T12:30:45.500Z:

  • YYYY-MM-DD — UTC calendar date
  • T — separator between date and time
  • HH:mm:ss.sss — UTC time with milliseconds
  • Z — Zulu time (UTC offset zero)

The same instant always produces the same ISO string worldwide—unlike local formatters that vary by timezone and locale.

💡
Beginner Tip

JSON.stringify({ at: new Date() }) uses toJSON(), which internally calls toISOString()—so ISO output is the default for Date objects in JSON.

📝 Syntax

JavaScript
dateObj.toISOString()

Parameters

  • None.

Return value

  • A UTC ISO 8601 string for the Date instant.
  • Throws RangeError: Invalid time value if the Date is invalid.

⚡ Quick Reference

GoalCode
Full UTC ISO stringdate.toISOString()
UTC date only YYYY-MM-DDdate.toISOString().slice(0, 10)
Parse back to timestampDate.parse(iso)
Validate before formatNumber.isNaN(date.getTime())
Local date labeldate.toDateString()
Now as ISOnew Date().toISOString()

📋 toISOString() vs Similar Methods

Pick the formatter that matches your storage and display needs.

toISOString()
UTC ISO

APIs / storage

toDateString()
local date

UI labels

Date.parse()
string→ms

Read pair

getTime()
epoch ms

Numeric storage

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Fixed examples use Date.UTC() for predictable UTC output.

📚 Getting Started

Serialize a fixed UTC instant as an ISO 8601 string.

Example 1 — Basic toISOString() Output

March 15, 2026 at 12:00 UTC becomes a standard ISO string with Z.

JavaScript
const date = new Date(Date.UTC(2026, 2, 15, 12, 0, 0));
const iso = date.toISOString();

console.log(iso); // "2026-03-15T12:00:00.000Z"
Try It Yourself

How It Works

The Z suffix confirms UTC. This string sorts lexicographically in chronological order.

📈 Practical Patterns

Compare formatters, API payloads, date extraction, and validation.

Example 2 — Compare toISOString() and toDateString()

Same instant, two views—UTC machine format vs local human date.

JavaScript
const date = new Date(2026, 2, 15, 14, 30, 0);

console.log(date.toISOString());   // UTC instant
console.log(date.toDateString());  // local date only
Try It Yourself

How It Works

Local 14:30 in UTC+5:30 becomes 09:00Z in ISO. Use ISO for storage; use toDateString for quick local labels.

Example 3 — Send an ISO Timestamp in an API Payload

JSON APIs expect portable UTC strings, not locale-specific text.

JavaScript
const meeting = new Date(Date.UTC(2026, 5, 20, 15, 0, 0));

const payload = {
  title: "Standup",
  scheduledAt: meeting.toISOString()
};

console.log(JSON.stringify(payload));
// {"title":"Standup","scheduledAt":"2026-06-20T15:00:00.000Z"}
Try It Yourself

How It Works

Servers worldwide can parse the ISO field without guessing timezone. JSON.stringify on a Date property would produce the same ISO value via toJSON().

Example 4 — Extract UTC Date Only with .slice(0, 10)

When you need YYYY-MM-DD for filters or CSV columns.

JavaScript
const date = new Date(Date.UTC(2026, 2, 15, 23, 59, 59));
const utcDateOnly = date.toISOString().slice(0, 10);

console.log(utcDateOnly); // "2026-03-15"
Try It Yourself

How It Works

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());
}
Try It Yourself

How It Works

Without the guard, bad.toISOString() throws RangeError: Invalid time value and can crash request handlers.

🚀 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.

📝 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().

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Date.toISOString() Excellent

Bottom line: Safe everywhere. Remember UTC output, RangeError on invalid dates, and slice for date-only.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.toISOString()

Your foundation for UTC ISO serialization in JavaScript.

5
Core concepts
🌐 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).

Continue to toJSON()

Learn how JSON.stringify serializes Date objects through the toJSON() hook.

toJSON() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful