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

Creating Dates from Date Strings

Pass ISO 8601 strings to new Date(string) and learn which formats are reliably parseable across environments.

Creating Dates from Date Strings

You'll learn how to create Date objects from string inputs and which string formats are reliably parseable across different JavaScript environments.

The Core Idea

While you've already learned to create dates from milliseconds (the epoch timestamp), you can also pass a date string directly to new Date(string). However, not all date strings are created equal — JavaScript's date parsing is notoriously inconsistent across browsers and environments, except for one format: ISO 8601.

The ISO 8601 Standard

ISO 8601 is an international standard for date/time representation. It looks like this:

// Full ISO 8601 format (safest)
const date1 = new Date('2024-03-15T10:30:00.000Z');

// Date only (interpreted as UTC midnight)
const date2 = new Date('2024-03-15');

// Date and time without timezone (local time)
const date3 = new Date('2024-03-15T10:30:00');

The format: YYYY-MM-DDTHH:mm:ss.sssZ where T separates date and time, and Z means UTC (or you can specify timezone offsets like +05:00).

Why ISO 8601 Matters

Other string formats ('March 15, 2024', '3/15/2024') might work, but their interpretation varies by browser and locale. Think of ISO 8601 as the universal language that all JavaScript environments speak fluently — anything else is a dialect that might be misunderstood.

// Reliable across all environments
const safe = new Date('2024-03-15T14:00:00Z');

// Risky — parsing varies by environment
const risky = new Date('03/15/2024'); // Avoid!

Key Takeaway: Always use ISO 8601 formatted strings (YYYY-MM-DD or full YYYY-MM-DDTHH:mm:ss.sssZ) when creating dates from strings to ensure consistent, predictable parsing across all JavaScript environments.