How to Format a Date as YYYY-MM-DD in JavaScript
Formatting a Date object into a YYYY-MM-DD string is one of the most common and essential date manipulation tasks. This specific format is the international standard (part of ISO 8601), making it perfect for database storage, API requests, and anywhere you need an unambiguous, naturally sortable date string.
This guide will teach you the most robust and reliable method for creating this format by manually extracting the date components. You will also learn how to adapt this logic to create other similar formats, like YYYYMMDD.
The Core Method: Manual Formatting with Padding
The most reliable way to format a date is to get each of its numeric components—the year, month, and day—and then join them together with the correct padding.
The logic:
- Get the Year: Use the
getFullYear()method to get the four-digit year. - Get the Month: Use the
getMonth()method. This method is zero-based (0 = January), so you must add 1 to get the correct month number. - Get the Day: Use the
getDate()method to get the day of the month (1-31). - Pad and Join: Ensure the month and day are two digits by padding them with a leading zero if necessary, then join the components with hyphens.
The Reusable Function (Best Practice)
For clean and maintainable code, you should encapsulate this logic in a reusable function.
For example, we have a Date object and we need to convert it into a YYYY-MM-DD string. The reusable function is the following:
function formatDate(date) {
const year = date.getFullYear();
// Add 1 to the month because it is 0-indexed
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
// Example Usage:
const myDate = new Date(2025, 6, 1);
console.log(formatDate(myDate)); // Output: "2025-07-01"
const today = new Date();
console.log(formatDate(today)); // Output: (e.g.) "2025-10-12"
How it works:
String(...).padStart(2, '0'): This is the key to the formatting. It converts the number to a string and then ensures the string is at least two characters long, adding a0to the start if necessary. This correctly handles a date like the 9th of the month, converting9to"09".
Adapting the Function for Other Formats (e.g., YYYYMMDD)
Once you have the core logic, creating other formats is trivial. To create a YYYYMMDD string, you simply change the final joiner from a hyphen to an empty string.
function formatDateAsYyyyMmDd(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
// The only change is the separator in the final string
return `${year}${month}${day}`;
}
// Example Usage:
const myDate = new Date(2025, 4, 9);
console.log(formatDateAsYyyyMmDd(myDate)); // Output: "20250509"
A Note on an Unreliable Alternative (toLocaleString)
You may see solutions using date.toLocaleString('sv-SE'), which happens to produce a YYYY-MM-DD format for the Swedish locale.
const myDate = new Date(2025, 4, 9);
// This works in many environments, but it's not guaranteed.
const result = myDate.toLocaleDateString('sv-SE');
console.log(result); // Output: "2025-05-09"
Why this is not recommended: The output of toLocaleString() and toLocaleDateString() is implementation-dependent and can vary across browsers and Node.js versions. Relying on a specific locale to produce a specific machine-readable format is a fragile hack. The manual formatting method is guaranteed to be consistent everywhere.
Conclusion
Formatting a Date object as a YYYY-MM-DD string is a fundamental task that is best solved with a manual, explicit approach.
- The most reliable method is to use
getFullYear(),getMonth() + 1, andgetDate()to get the date components. - Use
String().padStart(2, '0')to ensure the month and day are always two digits. - Encapsulate this logic in a reusable function for clean and maintainable code.
- Avoid locale-based shortcuts like
toLocaleString('sv-SE')for generating machine-readable date strings, as their output is not guaranteed to be consistent.