This is an archived page. It's not actively maintained.


非标准方法 toLocaleFormat() 按特定的格式将一个日期转换成一个字符串。 Intl.DateTimeFormat 是符合标准的格式化日期的替代方法。另见更新的(newer)版本的 Date.prototype.toLocaleDateString()方法.




与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.


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.