Date.prototype.setYear()
지원이 중단되었습니다: 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 the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Date
인스턴스의 setYear()
메서드는 현지 시간에 따른 특정 날짜의 연도를 설정합니다.
그러나 기존 setYear()
메서드가 연도 값을 설정하는 방식은 기본 설정된 setFullYear()
메서드가 연도 값을 설정하는 방식과 다르며 경우에 따라 new Date()
와 Date.parse()
의 연도 값 설정 방식과도 차이가 납니다. 특히 22
및 61
과 같은 두 자리 숫자가 주어진다면
setYear()
는 두 자리 숫자를1900
부터의 오프셋으로 해석합니다. 그래서date.setYear(22)
는 연도 값이1922
로 설정되고date.setYear(61)
는 연도 값이1961
로 설정됩니다. (반대로new Date(61, 1)
도 연도 값이1961
로 설정되지만new Date("2/1/22")
는 연도 값이2022
로 설정되며,Date.parse()
도 비슷하게 설정됩니다).setFullYear()
는 특별한 해석을 하지 않고 리터럴 두 자리 값을 그대로 사용하여 연도를 설정하므로date.setFullYear(61)
은 연도 값이0061
로 설정되고,date.setFullYear(22)
은 연도 값이0022
로 설정됩니다.
이러한 동작의 차이로 인해 더 이상 기존 setYear()
메서드를 사용하지 말고 기본 설정된 setFullYear()
메서드를 사용해야 합니다.
구문
setYear(yearValue)
매개변수
yearValue
-
정수.
반환 값
Date
객체를 즉시 변경하고 새 타임스탬프를 반환합니다. yearValue
가 NaN
(또는 undefined
와 같이 NaN
으로 강제로 변환되는 다른 값)이면 날짜가 유효하지 않은 날짜 로 설정되고 NaN
이 반환됩니다.
설명
yearValue
이 0 이상 99 이하의 숫자라면, dateObj
의 연도는 1900 + yearValue
으로 설정됩니다. 그렇지 않다면 dateObj
의 연도는 yearValue
으로 설정됩니다.
예제
setYear() 사용하기
처음 두 줄은 연도를 1996년으로 설정합니다. 세 번째 줄은 연도를 2000으로 설정합니다.
const theBigDay = new Date();
theBigDay.setYear(96);
theBigDay.setYear(1996);
theBigDay.setYear(2000);
명세서
Specification |
---|
ECMAScript Language Specification # sec-date.prototype.setyear |
브라우저 호환성
BCD tables only load in the browser