Date.prototype.toDateString()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

toDateString() 方法用於 Date 例項,返回一個字串,表示該日期在本地時區中解析的日期部分。

試一試

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
toDateString()

引數

無。

返回值

返回一個字串,表示給定日期的日期部分(格式請參閱描述)。如果日期 無效,則返回 "Invalid Date"

描述

Date 例項指的是一個特定的時間點。toDateString() 會在本地時區解析日期,並以英文格式化日期部分。它始終使用以下格式,各部分用空格分隔:

  1. 星期名稱的前三個字母
  2. 月份名稱的前三個字母
  3. 兩位數的月份日期,必要時左側用零填充
  4. 四位數的年份(至少),必要時左側用零填充。可能帶有負號

例如:"Thu Jan 01 1970"。

  • 如果您只想獲取時間部分,請使用 toTimeString()
  • 如果您想同時獲取日期和時間,請使用 toString()
  • 如果希望日期根據 UTC 而非本地時區進行解析,請使用 toUTCString()
  • 如果您想以更使用者友好的格式(例如,本地化)格式化日期,請使用 toLocaleDateString()

示例

使用 toDateString()

js
const d = new Date(0);

console.log(d.toString()); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)"
console.log(d.toDateString()); // "Thu Jan 01 1970"

規範

規範
ECMAScript® 2026 語言規範
# sec-date.prototype.todatestring

瀏覽器相容性

另見