Our volunteers haven't translated this article into Català yet. Join us and help get the job done!
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
The non-standard toLocaleFormat()
method converts a date to a string using the specified formatting. Intl.DateTimeFormat
is an alternative to format dates in a standards-compliant way. See also the newer version of Date.prototype.toLocaleDateString()
.
Syntax
dateObj.toLocaleFormat(formatString)
Parameters
formatString
- A format string in the same format expected by the
strftime()
function in C.
Return value
A string representing the given date using the specified formatting.
Description
The toLocaleFormat()
method provides greater software control over the formatting of the generated date and/or time. Names for months and days of the week are localized using the operating system's locale. However, ordering of the day and month and other localization tasks are not handled automatically since you have control over the order in which they occur. You should take care that the format string is localized properly according to the user's system settings. Be aware that the locale used is not necessarily the same as the locale of the browser.
Extension and XULRunner developers should know that just loading the format string from a .dtd
or .properties
file using a chrome://somedomain/locale/somefile.ext
URI should be avoided, as the .dtd
/.properties
file and the toLocaleFormat()
method does not not necessarily use the same locale, which could result in odd looking or even ambiguous or unreadable dates.
Also note that the behavior of the used locale depends on the platform, and the user might customize the locale used, so using the system locale the choose the format string might in some cases not even be adequate. You might consider using some of the more general toLocale*
methods of the Date
object or doing your own custom localization of the date to be displayed using some of the get*
methods of the Date
object instead of using this method.
Examples
Using toLocaleFormat()
var today = new Date(); var date = today.toLocaleFormat('%A, %B %e, %Y'); // Bad example
In this example, toLocaleFormat()
returns a string such as "Wednesday, October 3, 2007". Note that the format string in this example is not properly localized, which will result in the problems described above.
Polyfill
When using the DateJS library you can polyfill Date.prototype.toLocaleDateString()
like this:
if (!Date.prototype.toLocaleFormat) { (function() { Date.prototype.toLocaleFormat = function(formatString) { return this.format(formatString); }; }()); }
Specifications
Not part of any specification. Implemented in JavaScript 1.6.
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | ? | (Yes) | No support | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | (Yes) | ? | ? | ? |