From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/date/getday/index.html | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/date/getday') diff --git a/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html b/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html new file mode 100644 index 0000000000..20147639c4 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/date/getday/index.html @@ -0,0 +1,88 @@ +--- +title: Date.prototype.getDay() +slug: Web/JavaScript/Reference/Global_Objects/Date/getDay +translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDay +--- +

{{JSRef}}

+ +

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

+ +
{{EmbedInteractiveExample("pages/js/date-getday.html")}}
+ + + +

语法

+ +
dateObj.getDay()
+
+ +

返回值

+ +

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

+ +

例子

+ +

使用getDay()

+ +

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

+ +
var Xmas95 = new Date("December 25, 1995 23:15:30");
+var weekday = Xmas95.getDay();
+
+console.log(weekday); // 1
+ +
+

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

+ +
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
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范版本规范状态注解
{{SpecName('ESDraft', '#sec-date.prototype.getday', 'Date.prototype.getDay')}}  {{Spec2('ESDraft')}}  
{{SpecName('ES6', '#sec-date.prototype.getday', 'Date.prototype.getDay')}}    {{Spec2('ES6')}}
{{SpecName('ES5.1', '#sec-15.9.5.16', 'Date.prototype.getDay')}}        {{Spec2('ES5.1')}}
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.0.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Date.getDay")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf