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中被实现。
兼容性
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.
No compatibility data found. Please contribute data for "javascript.builtins.Date.toLocaleFormat" (depth: 1) to the MDN compatibility data repository.
另见
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论