Course contentsShow
← JavaScript
Lesson 1638 of 2,72335. Standard Library and Built-insFree lesson

toISOString() Always Returns UTC

Learn why toISOString() always outputs UTC time with 'Z' suffix, and how to handle local time serialization.

toISOString() Always Returns UTC

You'll learn why toISOString() always outputs time in UTC with a 'Z' suffix, regardless of your local timezone.

The UTC Guarantee

When you call toISOString() on a Date object, JavaScript always converts the date to UTC (Coordinated Universal Time) before formatting it. The result is an ISO 8601 string that ends with 'Z', which stands for "Zulu time" — another name for UTC.

This happens no matter what timezone your computer is in:

const date = new Date('2024-03-15T14:30:00'); // Local time (timezone-dependent)

console.log(date.toISOString());
// Output: "2024-03-15T19:30:00.000Z" (if you're in EST, UTC-5)
// The time was shifted by 5 hours to UTC

Notice the 'Z' at the end? That's your signal: this is UTC time, not local time.

Why This Matters for Serialization

When you need to send dates to a server or store them in JSON, toISOString() is perfect because it's unambiguous — everyone interprets UTC the same way. But if you need to serialize a date as local time, you'll need a different approach:

const date = new Date('2024-03-15T14:30:00');

// Wrong for local time preservation:
date.toISOString(); // "2024-03-15T19:30:00.000Z" — changed to UTC!

// For local time, build it manually:
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const localString = `${year}-${month}-${day}`;
// "2024-03-15" — preserves local interpretation

Key Takeaway: toISOString() always converts to UTC and appends 'Z'. If you need to preserve local time in a string, you must manually construct it using local getters like getFullYear(), getMonth(), and getDate().