非标准
该特性是非标准的,请尽量不要在生产环境中使用它!
非标准方法 toLocaleFormat()
按特定的格式将一个日期转换成一个字符串。 Date.prototype.toLocaleDateString()
方法.
语法
dateObj.toLocaleFormat(formatString)
参数
-
formatString
-
与C语言中的
strftime()
方法的参数形式要求相同的格式字符串。
描述
toLocaleFormat()
方法通过格式化生成的日期或时间提供了更好的软件层面的控制(provides greater software control over the formatting of the generated date and/or time)。用操作系统的地点来月份和星期几的名称本地化。然而,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 instead of using this method.
示例
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); }; }()); }
标准
不属于任何标准。在JavaScript 1.6中被实现。
兼容性
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toLocaleFormat
|
Chrome No support No | Edge No support No | Firefox No support 1.5 — 58 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support 4 — 58 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
Legend
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.