Date.prototype.getDay()

翻譯不完整。 請幫助我們翻譯這篇文章!

getDay() 方法會根據當地時間將指定日期返回一星期中的第幾天,其中0代表星期日。 在當月的某天可以參考Date.prototype.getDate()

語法

dateObj.getDay()

返回值

返回一個整數,數值介於0到6之間,取決於當地時間對應出指定日期為星期幾:0代表星期日,1代表星期一,2代表星期二,依此類推。

範例

使用 getDay()

下面第二行表示根據日期對象'Xmas95'的值,把1賦值給'weekday'。則1995年12月25日是星期一。

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

console.log(weekday); // 1

Note: 如果需要,可以使用Intl.DateTimeFormat加上options參數來獲取星期幾全名。使使用此方法能輕鬆做出各種國際語言:

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 (ECMA-262)
The definition of 'Date.prototype.getDay' in that specification.

瀏覽器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
getDayChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100

Legend

Full support  
Full support

See also