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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
---
title: Date.prototype.getYear()
slug: Web/JavaScript/Referencia/Objectes_globals/Date/getYear
translation_of: Web/JavaScript/Reference/Global_Objects/Date/getYear
---
<div>{{JSRef("Global_Objects", "Date")}} {{deprecated_header}}</div>
<h2 id="Summary" name="Summary">Resum</h2>
<p>El mètode<strong><code> getYear()</code></strong> retorna l'any de la data especificada segons l'hora local. Com que <code>getYear()</code> no retorna anys sencers ("el problema de l'any 2000"), no s'utilitza més i s'ha substituït pel mètode {{jsxref("Date.prototype.getFullYear", "getFullYear()")}}.</p>
<h2 id="Syntax" name="Syntax">Sintaxi</h2>
<pre class="syntaxbox"><code><var>dateObj</var>.getYear()</code></pre>
<h3 id="Parameters" name="Parameters">Paràmetres</h3>
<p>Cap.</p>
<h3 id="Returns" name="Returns">Retorna</h3>
<p>El mètode <code>getYear()</code> retorna l'any menys el valor 1900; és a dir:</p>
<ul>
<li>Per a anys majors o iguals a 2000, el valor retornat per <code>getYear()</code> és 100 o major. Per exemple, si l'any és el 2026, <code>getYear()</code> retorna 126.</li>
<li>Per a anys entre 1900 i 1999 (ambdós inclosos), el valor retornat per <code>getYear()</code> roman entre 0 i 99. Per exemple, per a l'any 1976 <code>getYear()</code> retorna 76.</li>
<li>Per a anys menors que 1900, el valor retornat per <code>getYear()</code> és menor que zero. Per exemple, per a l'any 1800, <code>getYear()</code> retorna -100.</li>
</ul>
<p>Per a tenir en compte anys abans o deprés del 2000 es recomana utilitzar {{jsxref("Date.prototype.getFullYear", "getFullYear()")}} en comptes de <code>getYear()</code> ja que retorna l'any sencer.</p>
<h2 id="Backward_compatibility" name="Backward_compatibility">Compatibilitat amb versions anteriors</h2>
<h3 id="JavaScript_1.2_and_earlier" name="JavaScript_1.2_and_earlier">Comportament en JavaScript 1.2 i anteriors</h3>
<p>El mètode <code>getYear()</code> retorna o bé un any amb dos dígits o bé un any amb 4 dígits:</p>
<ul>
<li>Per a anys entre 1900 i 1999 (ambdos inclosos) el valor retornat és l'any menys 1900. Per exemple, per a l'any 1976 el valor retornat és 76.</li>
<li>Per a anys menors que 1900 o majors que 1999 el valor retornat per <code>getYear()</code> és l'any amb 4 dígits. Per exemple, per a l'any 1856 el valor retornat és 1856. Per a l'any 2026 el valor retornat és 2026.</li>
</ul>
<h2 id="Examples" name="Examples">Exemples</h2>
<h3 id="Example:_Years_between_1900_and_1999" name="Example:_Years_between_1900_and_1999">Exemple: Anys entre1900 i 1999</h3>
<p>La segona sentència assigna el valor 95 a la variable <code>any</code>.</p>
<pre class="brush: js">var nadal = new Date('December 25, 1995 23:15:00');
var any = nadal.getYear(); // retorna 95
</pre>
<h3 id="Example:_Years_above_1999" name="Example:_Years_above_1999">Exemple: Anys majors que 1999</h3>
<p>La segona sentència assigna el valor 100 a la variable <code>any</code>.</p>
<pre class="brush: js">var nadal = new Date('December 25, 2000 23:15:00');
var any = Xmas.getYear(); // retorna 100
</pre>
<h3 id="Example:_Years_below_1900" name="Example:_Years_below_1900">Exemple: Anys menors que 1900</h3>
<p>La segona sentència assigna el valor -100 a la variable <code>any</code>.</p>
<pre class="brush: js">var nadal = new Date('December 25, 1800 23:15:00');
var any = nadal.getYear(); // retorna -100
</pre>
<h3 id="Example:_Setting_and_getting_a_year_between_1900_and_1999" name="Example:_Setting_and_getting_a_year_between_1900_and_1999">Exemple: Assignar i obtenir un any entre1900 i 1999</h3>
<p>La segona sentència assigna el valor 95 a la variable <code>any</code>, que representa l'any 1995.</p>
<pre class="brush: js">var nadal.setYear(95);
var any = nadal.getYear(); // retorna 95
</pre>
<h2 id="Especificacions">Especificacions</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Especificació</th>
<th scope="col">Estat</th>
<th scope="col">Comentaris</th>
</tr>
<tr>
<td>ECMAScript 1a Edició.</td>
<td>Standard</td>
<td>Definició inicial. Implementat a JavaScript 1.3.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-B.2.4', 'Date.prototype.getYear')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td>Definit a l'annex de compatibilitat (amb caràcter informatiu).</td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-date.prototype.getyear', 'Date.prototype.getYear')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Definit a l'annex de característiques adicionals per a navegadors web (amb caràcter normatiu).</td>
</tr>
</tbody>
</table>
<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Suport bàsic</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Característica</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Suport bàsic</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also" name="See_also">Vegeu també</h2>
<ul>
<li>{{jsxref("Date.prototype.getFullYear()")}}</li>
<li>{{jsxref("Date.prototype.getUTCFullYear()")}}</li>
<li>{{jsxref("Date.prototype.setYear()")}}</li>
</ul>
|