toTimeString()
方法以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
dateObj.toTimeString()
描述
SpiderMonkey 里,该字符串由日期部分(年月日)和其后的时间部分(时分秒和时区)组成。有时会需要获取时间部分的字符串,这可以由 toTimeString
方法完成。
The toTimeString
method is especially useful because compliant engines implementing ECMA-262 may differ in the string obtained from toString
for Date
objects, as the format is implementation-dependent; simple string slicing approaches may not produce consistent results across multiple engines.
例子
例子:toTimeString
方法的简单使用
var d = new Date(1993, 6, 28, 14, 39, 7); println(d.toString()); // prints Wed Jul 28 1993 14:39:07 GMT-0600 (PDT) println(d.toTimeString()); // prints 14:39:07 GMT-0600 (PDT)
规范
规范版本 | 规范状态 | 注解 |
---|---|---|
ECMAScript 3rd Edition | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) Date.prototype.toTimeString |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Date.prototype.toTimeString |
Standard |
浏览器兼容性
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.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toTimeString |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support