Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Date.prototype.toLocaleFormat()

这篇翻译不完整。请帮忙从英语翻译这篇文章

非标准
该特性是非标准的,请尽量不要在生产环境中使用它!

非标准方法 toLocaleFormat() 按特定的格式将一个日期转换成一个字符串。 Intl.DateTimeFormat 是符合标准的格式化日期的替代方法。另见更新的(newer)版本的 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 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.

示例

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中被实现。

兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? (Yes) 未实现 ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? (Yes) ? ? ?

另见

文档标签和贡献者

标签: 
 此页面的贡献者: charlie
 最后编辑者: charlie,