How to format date as YYYY-MM-DD in JavaScript
Formatting dates as YYYY-MM-DD is crucial for database storage, API communication, and HTML date inputs across web applications.
As the creator of CoreUI with over 25 years of development experience, I’ve handled date formatting in thousands of components and data processing scenarios.
The most reliable and widely compatible approach is using toISOString()
with string splitting to extract the date portion.
This method ensures consistent ISO 8601 formatting regardless of timezone complexities.
Use toISOString().split('T')[0]
to format any date as YYYY-MM-DD.
const formatted = new Date().toISOString().split('T')[0]
The toISOString()
method converts the date to UTC and returns an ISO string like “2025-10-02T08:00:00.000Z”. The split('T')[0]
extracts only the date part before the ‘T’ character, giving you “2025-10-02”. This approach handles all edge cases including month boundaries, leap years, and timezone differences automatically.
Best Practice Note:
This is the same method we use throughout CoreUI components for consistent date handling across different locales.
For local timezone dates, use manual formatting with getFullYear()
, getMonth() + 1
, and getDate()
instead.