toLocaleDateString
toLocaleDateString is a method of the JavaScript Date object that returns a string representing the date portion of a date, formatted according to locale-sensitive conventions. It relies on the Internationalization API (Intl) and adapts its output to the user’s locale and formatting options.
- dateObj.toLocaleDateString([locales[, options]])
- locales can be a string with a BCP 47 language tag or an array of such strings.
- options is an object that may include properties such as year, month, day, weekday, era, timeZone,
- The result is locale-sensitive, so the same Date can format differently in different locales.
- Example: const d = new Date(2024, 11, 25);
- d.toLocaleDateString('en-US') might yield "12/25/2024".
- d.toLocaleDateString('en-GB') might yield "25/12/2024".
- d.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }) could produce "December 25, 2024".
- If the Date object is invalid, some environments return a string like "Invalid Date" or throw.
- toLocaleDateString is intended for user-facing display and respects locale and regional preferences such as date order
- For stable, machine-readable formats, consider ISO 8601 strings or explicit Intl.DateTimeFormat usage with defined options.
- Behavior and supported options can vary across environments, so tests on target platforms are advisable.