DevLab
Time

ISO 8601 Explained: The Right Way to Format Dates

Learn the ISO 8601 date and datetime standard, why 2026-06-13T10:30:00Z is unambiguous, and how to use it correctly in APIs and databases.

The Problem with Dates

Is 06/07/2026 June 7th or July 6th? It depends on whether the author follows MM/DD/YYYY (US) or DD/MM/YYYY (most of the world). ISO 8601 eliminates all ambiguity by defining a single universal format.

The Format

Date only:         2026-06-13
Date + time:       2026-06-13T10:30:00
UTC time:          2026-06-13T10:30:00Z          (Z = UTC)
With offset:       2026-06-13T10:30:00+05:30     (India Standard Time)
With milliseconds: 2026-06-13T10:30:00.000Z

Always Use UTC in APIs

Store and transmit timestamps in UTC (Z suffix or +00:00). Convert to local time only at the display layer. This prevents bugs where a date stored in one timezone is interpreted differently when read back in another.

// JavaScript
new Date().toISOString()  // → "2026-06-13T10:30:00.000Z"

// Python
from datetime import datetime, timezone
datetime.now(timezone.utc).isoformat()

// PostgreSQL
SELECT NOW()::timestamptz;  -- returns UTC timestamp

Sortable as a String

ISO 8601's year-first ordering means dates sort correctly as plain strings: "2026-06-13" > "2026-01-01" is true in lexicographic comparison. No date parsing required for ordering — a significant advantage in logs, filenames, and database text columns.

Durations

P1Y       → 1 year
P3M       → 3 months
PT2H30M   → 2 hours, 30 minutes
P1DT12H   → 1 day, 12 hours

Parsing in JavaScript

const d = new Date("2026-06-13T10:30:00.000Z");
d.toLocaleDateString("en-IN", { timeZone: "Asia/Kolkata" });
// → "13/6/2026" (displayed in user's local timezone)

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines