Formatting Dates in JavaScript: Complete Guide
Learn how to format dates in JavaScript using toLocaleDateString, toISOString, and manual formatting. Practical examples for locale-aware display and API-ready strings.
JavaScript offers three ways to turn a Date object into a readable string. toLocaleDateString handles locale-aware display for users. toISOString produces machine-friendly UTC strings for storage. Manual formatting with get methods gives you full control over every character in the output.
const date = new Date(2026, 6, 14, 14, 30, 0);
console.log(date.toLocaleDateString("en-US"));
console.log(date.toISOString().slice(0, 10));The first prints "7/14/2026" in US format. The second prints "2026-07-14" by slicing just the date portion from the ISO string.
For the fundamentals of creating and reading dates, see the JavaScript Date object guide. For raw timestamp work, see the guide to working with timestamps.
Locale-aware formatting
toLocaleDateString formats a date for a specific locale. Pass a locale string like "en-US" or "de-DE" to control the output.
const date = new Date(2026, 6, 14);
console.log(date.toLocaleDateString("en-US"));
console.log(date.toLocaleDateString("de-DE"));US English prints "7/14/2026". German prints "14.7.2026". The same date renders in each locale's conventional format without you writing any formatting logic.
For richer output, pass an options object as the second argument. You can include the weekday, control month length, and specify numeric vs long representations.
const options = { weekday: "long", year: "numeric", month: "long", day: "numeric" };
console.log(date.toLocaleDateString("en-US", options));This prints "Tuesday, July 14, 2026". Available options include weekday, year, month, day, hour, minute, second, and timeZoneName. Each accepts values like "long", "short", "narrow", or "numeric".
The related method toLocaleString adds the time portion to the output.
Machine-readable formatting
toISOString returns a UTC-based ISO 8601 string. This is the format for databases, APIs, and JSON serialization because it is universally parseable and sorts correctly as a string.
const now = new Date();
console.log(now.toISOString());This prints "2026-07-14T18:30:00.000Z". The Z suffix means UTC time. For just the date portion, slice the first 10 characters. For a timestamp in milliseconds, use getTime or Date.now instead.
Manual formatting
When locale methods cannot produce the exact format you need, build the string from individual date components. Use getFullYear, getMonth (plus 1), and getDate.
function formatDate(date) {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return `${y}-${m}-${d}`;
}
console.log(formatDate(new Date(2026, 0, 5)));This prints "2026-01-05". The padStart calls ensure January (month 0 + 1 = 1) becomes "01" and the 5th becomes "05". Without padding, you would get "2026-1-5".
For a readable format like "Jul 14, 2026", use an array of abbreviated month names indexed by getMonth. Since getMonth returns 0 for January and the array is zero-indexed, no adjustment is needed.
Common mistakes
Forgetting to add 1 to getMonth is the most frequent date formatting bug. getMonth returns 0 for January through 11 for December. Always write date.getMonth() + 1 in display code.
Assuming toISOString returns local time is incorrect. It always returns UTC. If you need a local-time ISO-like string, build it manually with the local get methods.
Using toLocaleDateString without an explicit locale argument produces different output on different machines. Specify a locale when consistency matters across environments.
For parsing JSON date strings back into Date objects, see the guide to JSON parse and stringify.
Rune AI
Key Insights
- toLocaleDateString formats a date for the user's locale with minimal effort.
- toISOString produces a UTC-based ISO 8601 string ideal for storage and APIs.
- Combine getFullYear, getMonth (+1), and getDate with padStart for custom formats.
- Always add 1 to getMonth before displaying it. Months are zero-indexed.
- Use toISOString().slice(0, 10) for a quick YYYY-MM-DD date string.
Frequently Asked Questions
How do I get a date in YYYY-MM-DD format?
Does toLocaleDateString work in Node.js?
How do I add leading zeros to month and day?
Conclusion
JavaScript provides three approaches to date formatting. Use toLocaleDateString for human-readable, locale-aware display. Use toISOString for machine-readable UTC strings ideal for storage and APIs. Build custom formats manually with getFullYear, getMonth, and getDate when you need full control over every detail of the output.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.