此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Date.prototype.toDateString()

基线
广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

toDateString() 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串。

尝试一下

const event = new Date(1993, 6, 28, 14, 39, 7);

console.log(event.toString());
// Expected output: "Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)"
// Note: your timezone may vary

console.log(event.toDateString());
// Expected output: "Wed Jul 28 1993"

语法

js
dateObj.toDateString()

描述

Date 对象实例引用一个具体的时间点。调用 toString 方法会以美式英语和人类易读的形式返回日期对象的格式化字符串。该字符串由日期部分(年月日)和其后的时间部分(时分秒及时区)组成。有时需要获取日期部分的字符串,这可以由 toDateString 方法完成。

toDateString() 方法特别有用,因为符合 ECMA-262 的引擎对 Date 对象调用 toString 所得到的字符串可能各不相同:该格式取决于具体实现,因此简单地截取字符串在不同引擎之间未必能得到一致的结果。

示例

示例:toDateString 方法的简单使用

js
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.toDateString()); // prints Wed Jul 28 1993

规范

规范
ECMAScript® 2027 Language Specification
# sec-date.prototype.todatestring

浏览器兼容性

参见