Date.prototype.getDay()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天。对于某个月中的第几天,参考Date.prototype.getDate().

尝试一下

const birthday = new Date("August 19, 1975 23:15:30");
const day1 = birthday.getDay();
// Sunday - Saturday : 0 - 6

console.log(day1);
// Expected output: 2

语法

js
getDay()

返回值

根据本地时间,返回一个 0 到 6 之间的整数值,代表星期几:0 代表星期日,1 代表星期一,2 代表星期二,依次类推。

示例

使用getDay()

下面第二条语句,基于Date对象 Xmas95 的值,把 1 赋值给 weekday。也就是说 1995 年 12 月 25 日是星期一。

js
var Xmas95 = new Date("December 25, 1995 23:15:30");
var weekday = Xmas95.getDay();

console.log(weekday); // 1

备注: 如果需要,可以使用Intl.DateTimeFormat与一个额外的options 参数,从而返回这天的全称(如"Monday").使用此方法,结果会更加国际化:

js
var options = { weekday: "long" };
console.log(new Intl.DateTimeFormat("en-US", options).format(Xmas95));
// Monday
console.log(new Intl.DateTimeFormat("de-DE", options).format(Xmas95));
// Montag

规范

Specification
ECMAScript® 2025 Language Specification
# sec-date.prototype.getday

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
getDay

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见