Date.prototype.toString()
Date 实例的 toString() 方法返回一个表示在本地时区解释的日期的字符串。
¥The toString() method of Date instances returns a string representing this date interpreted in the local timezone.
Try it
语法
参数
返回值
描述
¥Description
toString() 方法是 类型强制协议 的一部分。由于 Date 有一个 [Symbol.toPrimitive]() 方法,因此当 Date 对象隐式为 强制为字符串 时,该方法始终优先于 toString()。然而,Date.prototype[Symbol.toPrimitive]() 仍然在内部调用 this.toString()。
¥The toString() method is part of the type coercion protocol. Because Date has a [Symbol.toPrimitive]() method, that method always takes priority over toString() when a Date object is implicitly coerced to a string. However, Date.prototype[Symbol.toPrimitive]() still calls this.toString() internally.
Date 对象覆盖 Object 的 toString() 方法。Date.prototype.toString() 返回本地时区中解释的日期的字符串表示形式,包含日期和时间 - 它将 toDateString() 和 toTimeString() 中指定的字符串表示形式连接在一起,并在中间添加一个空格。例如:"1970 年 1 月 1 日星期四 00:00:00 GMT+0000(协调世界时)"。
¥The Date object overrides the toString() method of Object. Date.prototype.toString() returns a string representation of the Date as interpreted in the local timezone, containing both the date and the time — it joins the string representation specified in toDateString() and toTimeString() together, adding a space in between. For example: "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)".
必须在 Date 实例上调用 Date.prototype.toString()。如果 this 值不是从 Date.prototype 继承的,则抛出 TypeError。
¥Date.prototype.toString() must be called on Date instances. If the this value does not inherit from Date.prototype, a TypeError is thrown.
- 如果只想获取日期部分,请使用
toDateString()。 - 如果你只想获取时间部分,请使用
toTimeString()。 - 如果要将日期解释为 UTC 而不是本地时区,请使用
toUTCString()。 - 如果你想以更用户友好的格式(例如本地化)设置日期格式,请使用
toLocaleString()。
示例
使用 toString()
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-date.prototype.tostring |
浏览器兼容性
BCD tables only load in the browser