aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/date/getday/index.html
blob: abdc0e89a1c59f1d37660dd79c2d925aa2473446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: Date.prototype.getDay()
slug: Web/JavaScript/Reference/Global_Objects/Date/getDay
translation_of: Web/JavaScript/Reference/Global_Objects/Date/getDay
---
<div>{{JSRef}}</div>

<p><span class="seoSummary"><strong><code>getDay()</code></strong> 方法會根據當地時間將指定日期返回一星期中的第幾天,</span>其中0代表星期日。 在當月的某天可以參考{{jsxref("Date.prototype.getDate()")}}</p>

<div>{{EmbedInteractiveExample("pages/js/date-getday.html", "shorter")}}</div>

<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>

<h2 id="語法">語法</h2>

<pre class="syntaxbox notranslate"><code><var>dateObj</var>.getDay()</code></pre>

<h3 id="返回值">返回值</h3>

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

<h2 id="範例">範例</h2>

<h3 id="使用_getDay">使用 <code>getDay()</code></h3>

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

<pre class="brush: js notranslate">var Xmas95 = new Date('December 25, 1995 23:15:30');
var weekday = Xmas95.getDay();

console.log(weekday); // 1
</pre>

<div class="blockIndicator note">
<p><strong>Note:</strong> 如果需要,可以使用{{jsxref("DateTimeFormat", "Intl.DateTimeFormat")}}加上<code>options</code>參數來獲取星期幾全名。使使用此方法能輕鬆做出各種國際語言:</p>

<pre class="brush: js notranslate">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
</pre>
</div>

<h2 id="規範">規範</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-date.prototype.getday', 'Date.prototype.getDay')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器兼容性">瀏覽器兼容性</h2>



<p>{{Compat("javascript.builtins.Date.getDay")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Date.prototype.getUTCDate()")}}</li>
 <li>{{jsxref("Date.prototype.getUTCDay()")}}</li>
 <li>{{jsxref("Date.prototype.setDate()")}}</li>
</ul>