Date.prototype.getYear()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Date/getYear
  • Revision title: Date.prototype.getYear()
  • Revision id: 485845
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Date")}} {{Deprecated_header("")}}

Summary

The getYear() method returns the year in the specified date according to local time. Because getYear does not return full years ("year 2000 problem"), it is no longer used and has been replaced by the {{jsxref("Date.getFullYear", "getFullYear")}} method.

Syntax

dateObj.getYear() 

Parameters

None.

Returns

The getYear method returns the year minus 1900; thus:

  • For years greater than or equal to 2000, the value returned by getYear is 100 or greater. For example, if the year is 2026, getYear returns 126.
  • For years between and including 1900 and 1999, the value returned by getYear is between 0 and 99. For example, if the year is 1976, getYear returns 76.
  • For years less than 1900, the value returned by getYear is less than 0. For example, if the year is 1800, getYear returns -100.

To take into account years before and after 2000, you should use {{jsxref("Date.getFullYear", "getFullYear()")}} instead of getYear so that the year is specified in full.

Backward Compatibility

Behaviour in JavaScript 1.2 and earlier

The getYear method returns either a 2-digit or 4-digit year:

  • For years between and including 1900 and 1999, the value returned by getYear is the year minus 1900. For example, if the year is 1976, the value returned is 76.
  • For years less than 1900 or greater than 1999, the value returned by getYear is the four-digit year. For example, if the year is 1856, the value returned is 1856. If the year is 2026, the value returned is 2026.

Examples

Example: Years between 1900 and 1999

The second statement assigns the value 95 to the variable year.

var Xmas = new Date("December 25, 1995 23:15:00");
var year = Xmas.getYear(); // returns 95

Example: Years above 1999

The second statement assigns the value 100 to the variable year.

var Xmas = new Date("December 25, 2000 23:15:00");
var year = Xmas.getYear(); // returns 100

Example: Years below 1900

The second statement assigns the value -100 to the variable year.

var Xmas = new Date("December 25, 1800 23:15:00");
var year = Xmas.getYear(); // returns -100

Example: Setting and getting a year between 1900 and 1999

The second statement assigns the value 95 to the variable year, representing the year 1995.

var Xmas.setYear(95);
var year = Xmas.getYear(); // returns 95 

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
{{SpecName('ES5.1', '#sec-B.2.4', 'Date.prototype.getYear')}} {{Spec2('ES5.1')}} Defined in the (informative) compatibility annex.
{{SpecName('ES6', '#sec-date.prototype.getyear', 'Date.prototype.getYear')}} {{Spec2('ES6')}} Defined in the (normative) annex for additional features for web browsers.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("Date.prototype.getFullYear()")}}
  • {{jsxref("Date.prototype.getUTCFullYear()")}}
  • {{jsxref("Date.prototype.setYear()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Date")}} {{Deprecated_header("")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The getYear() method returns the year in the specified date according to local time. Because <code>getYear</code> does not return full years ("year 2000 problem"), it is no longer used and has been replaced by the {{jsxref("Date.getFullYear", "getFullYear")}} method.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>dateObj.getYear() </code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>
<h3 id="Description" name="Description">Returns</h3>
<p>The <code>getYear</code> method returns the year minus 1900; thus:</p>
<ul>
 <li>For years greater than or equal to 2000, the value returned by <code>getYear</code> is 100 or greater. For example, if the year is 2026, <code>getYear</code> returns 126.</li>
 <li>For years between and including 1900 and 1999, the value returned by <code>getYear</code> is between 0 and 99. For example, if the year is 1976, <code>getYear</code> returns 76.</li>
 <li>For years less than 1900, the value returned by <code>getYear</code> is less than 0. For example, if the year is 1800, <code>getYear</code> returns -100.</li>
</ul>
<p>To take into account years before and after 2000, you should use {{jsxref("Date.getFullYear", "getFullYear()")}} instead of <code>getYear</code> so that the year is specified in full.</p>
<h2 id="Backward_Compatibility" name="Backward_Compatibility">Backward Compatibility</h2>
<h3 id="JavaScript_1.2_and_earlier" name="JavaScript_1.2_and_earlier">Behaviour in JavaScript 1.2 and earlier</h3>
<p>The <code>getYear</code> method returns either a 2-digit or 4-digit year:</p>
<ul>
 <li>For years between and including 1900 and 1999, the value returned by <code>getYear</code> is the year minus 1900. For example, if the year is 1976, the value returned is 76.</li>
 <li>For years less than 1900 or greater than 1999, the value returned by <code>getYear</code> is the four-digit year. For example, if the year is 1856, the value returned is 1856. If the year is 2026, the value returned is 2026.</li>
</ul>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Years_between_1900_and_1999" name="Example:_Years_between_1900_and_1999">Example: Years between 1900 and 1999</h3>
<p>The second statement assigns the value 95 to the variable <code>year</code>.</p>
<pre class="brush:js">
var Xmas = new Date("December 25, 1995 23:15:00");
var year = Xmas.getYear(); // returns 95
</pre>
<h3 id="Example:_Years_above_1999" name="Example:_Years_above_1999">Example: Years above 1999</h3>
<p>The second statement assigns the value 100 to the variable <code>year</code>.</p>
<pre class="brush:js">
var Xmas = new Date("December 25, 2000 23:15:00");
var year = Xmas.getYear(); // returns 100
</pre>
<h3 id="Example:_Years_below_1900" name="Example:_Years_below_1900">Example: Years below 1900</h3>
<p>The second statement assigns the value -100 to the variable <code>year</code>.</p>
<pre class="brush:js">
var Xmas = new Date("December 25, 1800 23:15:00");
var year = Xmas.getYear(); // returns -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">Example: Setting and getting a year between 1900 and 1999</h3>
<p>The second statement assigns the value 95 to the variable <code>year</code>, representing the year 1995.</p>
<pre class="brush:js">
var Xmas.setYear(95);
var year = Xmas.getYear(); // returns 95 
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-B.2.4', 'Date.prototype.getYear')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Defined in the (informative) compatibility annex.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-date.prototype.getyear', 'Date.prototype.getYear')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Defined in the (normative) annex for additional features for web browsers.</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</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>Feature</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>Basic support</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">See also</h2>
<ul>
 <li>{{jsxref("Date.prototype.getFullYear()")}}</li>
 <li>{{jsxref("Date.prototype.getUTCFullYear()")}}</li>
 <li>{{jsxref("Date.prototype.setYear()")}}</li>
</ul>
Revert to this revision