Date.prototype.toLocaleFormat() - JavaScript 编辑

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

非标准方法 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中被实现。

兼容性

No compatibility data found. Please contribute data for "javascript.builtins.Date.toLocaleFormat" (depth: 1) to the MDN compatibility data repository.

另见

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:77 次

字数:5660

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文