Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Date
  6. getYear()

Date.prototype.getYear()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

ThegetYear() method ofDate instances returns the year for this date according to local time. BecausegetYear() does not return full years ("year 2000 problem"), it is deprecated and has been replaced by thegetFullYear() method.

Syntax

js
getYear()

Parameters

None.

Return value

An integer representing the year for the given date according to local time, minus 1900. ReturnsNaN if the date isinvalid.

  • For years greater than or equal to 2000, the value 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 bygetYear() is between 0 and 99. For example, if the year is 1976,getYear() returns 76.
  • For years less than 1900, the value returned bygetYear() is less than 0. For example, if the year is 1800,getYear() returns -100.

This method essentially returns the value ofgetFullYear() minus 1900. You should usegetFullYear() instead, so that the year is specified in full.

Examples

Years between 1900 and 1999

The second statement assigns the value 95 to the variableyear.

js
const xmas = new Date("1995-12-25");const year = xmas.getYear(); // returns 95

Years above 1999

The second statement assigns the value 100 to the variableyear.

js
const xmas = new Date("2000-12-25");const year = xmas.getYear(); // returns 100

Years below 1900

The second statement assigns the value -100 to the variableyear.

js
const xmas = new Date("1800-12-25");const year = xmas.getYear(); // returns -100

Setting and getting a year between 1900 and 1999

The third statement assigns the value 95 to the variableyear, representing the year 1995.

js
const xmas = new Date("2015-12-25");xmas.setYear(95);const year = xmas.getYear(); // returns 95

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-date.prototype.getyear

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp