非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。
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()
.
This feature has been removed and will no longer work in Firefox 58+. 见 Warning: Date.prototype.toLocaleFormat is deprecated for more information and migration help.
dateObj.toLocaleFormat(formatString)
formatString
strftime()
function in C.
A string representing the given date using the specified formatting.
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.
toLocaleFormat()
var today = new Date();
var date = today.toLocaleFormat('%A, %B %e, %Y');
在此范例中,
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.
当使用
DateJS
library you can polyfill
Date.prototype.toLocaleDateString()
like this:
if (!Date.prototype.toLocaleFormat) {
(function() {
Date.prototype.toLocaleFormat = function(formatString) {
return this.format(formatString);
};
}());
}
Not part of any standard.
Not supported in any browser. Previously supported in Firefox 1.5 until Firefox 57 (January 2018).
Intl.DateTimeFormat
Date.prototype.toLocaleString()
Date.prototype.toLocaleDateString()
Date.prototype.toLocaleTimeString()
Archive
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()