How to Format a Date as YYYY-MM-DD HH:MM:SS in JavaScript
A very common requirement in programming is to format a Date object into a specific string format like YYYY-MM-DD HH:MM:SS. This standardized, sortable format is ideal for databases, log files, and API payloads. While you could build this string manually, the modern and recommended approach is to use the built-in Intl.DateTimeFormat API, which is powerful, flexible, and locale-aware.
This guide will teach you how to use Intl.DateTimeFormat to achieve this specific format. We will also cover the traditional manual method to give you a complete understanding of date formatting in JavaScript.
The Core Problem: No Simple .format() Method
Unlike in some other languages, the native JavaScript Date object does not have a simple, built-in formatting method like date.format('YYYY-MM-DD'). Methods like .toString() or .toLocaleString() provide pre-defined formats, but they don't offer the granular control needed for a custom format like YYYY-MM-DD HH:MM:SS.
The Modern Method (Recommended): Intl.DateTimeFormat
The Intl (Internationalization) API is the standard tool for locale-sensitive string formatting, including dates and times. It is the best choice for this task because it handles time zones and formatting conventions correctly.
For example, you have a Date object and need to convert it to the YYYY-MM-DD HH:MM:SS string format.
// Problem: How to format this Date object into a specific string?
const myDate = new Date();
Solution: by using a specific locale and a detailed options object, you can construct the desired format.
function formatDate(date) {
// Use a locale that gives us the YYYY-MM-DD format, like 'sv-SE' (Swedish).
// Use `hour12: false` to get 24-hour time.
const options = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
};
// Create a formatter for a locale that uses the desired format structure.
const formatter = new Intl.DateTimeFormat('sv-SE', options);
const formattedDate = formatter.format(date);
return formattedDate;
}
// Example Usage:
console.log(formatDate(new Date()));
Output:
2025-10-12 18:45:27
How it works: We cleverly use the Swedish (sv-SE) locale because its default date and time structure matches our target format. The options object ensures each component is a zero-padded, 2-digit number.
The Manual Method: Building the String Component by Component
While not recommended for most cases, you can always construct the string manually by extracting each part of the Date object. This gives you absolute control but is more verbose and not locale-aware.
function formatDateManually(date) {
// Helper function to pad numbers with a leading zero
const pad = (num) => num.toString().padStart(2, '0');
const year = date.getFullYear();
const month = pad(date.getMonth() + 1); // getMonth() is zero-based (0-11)
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// Example Usage:
console.log(formatDateManually(new Date()));
Output:
2025-10-12 18:45:27
The main pitfalls of this method are remembering that .getMonth() is zero-based and manually handling the zero-padding with a helper function like padStart.
Why the Intl API is the Best Practice
The Intl.DateTimeFormat API is superior to manual formatting for several reasons:
- Locale-Aware: It can easily be adapted to other formats (e.g.,
MM/DD/YYYYforen-US) just by changing the locale string, without rewriting the logic. - Time Zone Correctness: It has robust support for displaying dates in different time zones, which is very difficult to handle manually.
- Maintained by the Browser: It is a standard API that is highly optimized and maintained by browser vendors.
For a fixed, machine-readable format like YYYY-MM-DD, the manual method is acceptable. For any format that will be displayed to a user, the Intl API is the professional choice.
Conclusion
Formatting a Date object to the YYYY-MM-DD HH:MM:SS format is a common and solvable problem in JavaScript.
- The recommended best practice is to use the
Intl.DateTimeFormatAPI. By selecting an appropriate locale (sv-SE) and configuring the options, you can generate the desired format in a robust and flexible way. - The manual method of building the string from individual components (
.getFullYear(),.getMonth() + 1, etc.) is a functional alternative that gives you complete control but is more verbose and less flexible.
For modern, maintainable, and internationally-aware applications, the Intl API is the superior tool for all date formatting tasks.