This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Date.prototype.getYear()

Deprecated

Avoid using this feature in new projects. The getYear() and setYear() methods use a truncated number instead of the actual year number, known as a year 2000 problem. This feature may be a candidate for removal from web standards or browsers.

Date 인스턴스의 getYear() 메서드는 현지 시간에 따라 이 날짜의 연도를 반환합니다. getYear()는 전체 연도("2000년도 문제")를 반환하지 않기 때문에 더 이상 사용되지 않으며 getFullYear() 메서드로 대체되었습니다.

구문

js
getYear()

매개변수

없음.

반환 값

현지 시간 기준 지정된 날짜의 연도를 나타내는 정수에서 1900을 뺀 값입니다. 날짜가 유효하지 않은 경우 NaN을 반환합니다.

  • 연도가 2000보다 크거나 같은 경우 값은 100보다 큽니다. 예를 들어 연도가 2026인 경우 getYear()는 126을 반환합니다.
  • 1900년부터 1999년까지 연도의 경우 getYear()가 반환하는 값은 0에서 99 사이입니다. 예를 들어 연도가 1976인 경우 getYear()는 76을 반환합니다.
  • 1900보다 작은 연도의 경우 getYear()가 반환하는 값은 0보다 작습니다. 예를 들어 연도가 1800인 경우 getYear()는 -100을 반환합니다.

이 메서드는 기본적으로 getFullYear()의 값에서 1900을 뺀 값을 반환합니다. 대신 전체 연도가 명시된 getFullYear()를 사용해야 합니다.

예제

1900과 1999 사이의 연도

두 번째 문은 변수 year에 95를 할당합니다.

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

1999 이후의 연도

두 번째 문은 변수 year에 100을 할당합니다.

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

1900년 미만의 연도

두 번째 문은 변수 year에 -100을 할당합니다.

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

1900과 1999 사이의 연도 설정하기 및 가져오기

세 번째 문은 1995년을 나타내기 위해 변수 year에 95를 할당합니다.

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

명세서

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

브라우저 호환성

같이 보기