aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/date/setyear/index.html
blob: 7999ad0022a39ad918b81dfa6514faf13d02de84 (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.setYear()
slug: Web/JavaScript/Reference/Global_Objects/Date/setYear
tags:
  - JavaScript
  - 已废弃
  - 日期
translation_of: Web/JavaScript/Reference/Global_Objects/Date/setYear
---
<div>{{JSRef}} {{deprecated_header}}</div>

<p><strong><code>setYear()</code></strong> 方法根据一个本地时间为一个确定的日期对象设置年份。由于<code>setYear()</code> 并不设置完整年份("正是千年虫问题"),本方法已经完全被{{jsxref("Date.prototype.setFullYear()","setFullYear()")}} 方法所取代。</p>

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

<pre class="syntaxbox"><code><var>dateObj</var>.setYear(<var>yearValue</var>)</code></pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>yearValue</code></dt>
 <dd>一个整数。</dd>
</dl>

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

<p>介于1970年1月1日 00:00:00 UTC时间与更新后日期的毫秒数。</p>

<h2 id="描述">描述</h2>

<p>如果<code>yearValue</code> 是介于0到99(包含99)之间的整数,则目标对象 <code>dateObj</code> 的年份被设置为 <code>1900 + yearValue</code>。否则,目标对象 <code>dateObj</code> 的年份被设置为 <code>yearValue</code>.</p>

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

<h3 id="使用_setYear">使用 <code>setYear()</code></h3>

<p>例子前两行(除去声明)将年份设置为1996。 第三行将年份设置为2000.</p>

<pre class="brush: js">var theBigDay = new Date();

theBigDay.setYear(96);
theBigDay.setYear(1996);
theBigDay.setYear(2000);
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-date.prototype.setyear', 'Date.prototype.setYear')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

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

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("Date.prototype.getFullYear()")}}</li>
 <li>{{jsxref("Date.prototype.getUTCFullYear()")}}</li>
 <li>{{jsxref("Date.prototype.setFullYear()")}}</li>
 <li>{{jsxref("Date.prototype.setUTCFullYear()")}}</li>
</ul>