programing

Javascript getYear ()가 108을 반환하는 이유는 무엇입니까?

nasanasas 2020. 8. 30. 08:42
반응형

Javascript getYear ()가 108을 반환하는 이유는 무엇입니까?


이 자바 스크립트가 2008 대신 108을 반환하는 이유는 무엇입니까? 날짜와 월은 정확하지만 연도는 맞지 않습니까?

myDate = new Date();
year = myDate.getYear();

연도 = 108?


그것은 Y2K 일이며 1900 년 이후로만 계산됩니다.

이제 잠재적 인 호환성 문제는 getYear()찬성 사용되지 않습니다 getFullYear()-에서 쿼크 모드는 :

문제를 더 복잡하게 만들기 위해 date.getYear ()는 현재 더 이상 사용되지 않으며 date.getFullYear ()를 사용해야하며 이는 이전 브라우저에서 지원되지 않습니다. 그러나 작동한다면 항상 1 년을 제공해야합니다. 100 대신 2000.

브라우저는 다음 두 가지 방법으로 다음 해를 제공합니다.

* The year according to getYear(): 108
* The year according to getFullYear(): 2008

의 IE의 구현으로 인터넷 익스플로러와 파이어 폭스 간의 구현 차이도있다 getYear()변경되었습니다처럼 행동 getFullYear()-에서 IBM :

ECMAScript 사양에 따라 getYear는 1900 년을 뺀 연도를 반환하며 원래는 1998 년에 "98"을 반환하는 것을 의미했습니다. getYear는 ECMAScript 버전 3에서 더 이상 사용되지 않으며 getFullYear ()로 대체되었습니다.

Internet Explorer는 getYear ()를 getFullYear ()처럼 작동하고 Y2k를 준수하도록 변경했으며 Mozilla는 표준 동작을 유지했습니다.


getFullYear는 이전 브라우저에서 작동하지 않으므로 다음과 같이 사용할 수 있습니다.

Date.prototype.getRealYear = function() 
{ 
    if(this.getFullYear)
        return this.getFullYear();
    else
        return this.getYear() + 1900; 
};

Javascript 프로토 타입은 C # 확장 메서드와 마찬가지로 기존 개체를 확장하는 데 사용할 수 있습니다. 이제 우리는 이것을 할 수 있습니다.

var myDate = new Date();
myDate.getRealYear();
// Outputs 2008

문서를 확인하십시오. Y2K 문제가 아니라 Y2K 문제가 없습니다! 이 결정은 원래 C로 이루어졌으며 Perl, JavaScript 및 기타 여러 언어로 복사되었습니다. 오래 전에는 두 자리 연도를 사용하는 것이 여전히 바람직하다고 느꼈지만, 그 인터페이스를 설계 한 사람은 누구나 2000 년 이후에 일어날 일에 대해 생각할 필요가 있다는 것을 충분히 미리 알고 있었기 때문에 마지막 두 자리를 제공하는 대신 숫자, 그들은 1900 년 이후의 연수를 제공했습니다. 급하거나 위험하기를 원한다면 두 숫자를 사용할 수 있습니다. 또는 프로그램이 계속 작동하도록하려면 결과에 100을 더하고 본격적인 4 자리 연도를 사용할 수 있습니다.

Perl에서 처음으로 날짜 조작을했던 때를 기억합니다. 이상하게도 나는 문서를 읽었다 . 분명히 이것은 흔한 일이 아닙니다. 1 ~ 2 년 후 저는 1999 년 12 월 31 일 사무실로 전화를 걸어서 계약 펄 코드에서 마지막 순간에 발견 된 버그를 수정했습니다. 바로이 문제였습니다. 표준 날짜 호출은 1900 년 이후로 연도를 반환했으며 프로그래머는이를 두 자리 연도로 취급했습니다. (그들은 2000 년에 "00"을받을 것이라고 생각했습니다.) 경험이없는 젊은 프로그래머로서 "전문적인"직업에 너무 많은 추가 비용을 지불했고 그 사람들은이 책을 읽는 것을 귀찮게하지 않았다는 사실이 제 마음을 사로 잡았습니다. 선적 서류 비치. 그것은 수년간의 환멸의 시작이었습니다. 이제 나는 늙고 냉소적입니다. :)

2000 년에, 연례 YAPC Perl 컨퍼런스는 자주보고되는이 버그가 아닌 것을 기리기 위해 "YAPC 19100"이라고 불 렸습니다.

요즘에는 적어도 Perl 세계에서는 실제 4 자리 연도를 사용하는 날짜 처리를 위해 표준 모듈을 사용하는 것이 더 합리적입니다. JavaScript에서 사용할 수있는 항목이 확실하지 않습니다.


1900 년 이후의 연도를 반환해야합니다.


를 사용하십시오 date.getFullYear().

이것은 (다른 곳에서 올바르게 지적했듯이) Y2K 일입니다. 넷스케이프는 (2000 년 이전에 작성된)는 원래 예를 들어, 반환 98에서 getYear(). 로 돌아 가지 00않고 대신 1002000 년에 돌아 왔습니다 . 그런 다음 다른 브라우저가 등장하여 다른 방식으로 작업을 수행했으며 비 호환성이 지배함에 따라 모두가 불행했습니다.

이후 브라우저 getFullYear는 전체 연도를 반환하는 표준 방법으로 지원 됩니다.


This question is so old that it makes me weep with nostalgia for the dotcom days!

That's right, Date.getYear() returns the number of years since 1900, just like Perl's localtime(). One wonders why a language designed in the 1990s wouldn't account for the century turnover, but what can I say? You had to be there. It sort of made a kind of sense at the time (like pets.com did).

Before 2000, one might have been tempted to fix this bug by appending "19" to the result of getYear() resulting in the "year 19100 bug". Others have already answered this question sufficiently (add 1900 to the result of getDate()).

Maybe the book you're reading about JavaScript is a little old?

Thanks for the blast from the past!


You should, as pointed out, never use getYear(), but instead use getFullYear().

The story is however not as simple as "IE implements GetYear() as getFullYear(). Opera and IE these days treat getYear() as getYear() was originally specified for dates before 2000, but will treat it as getFullYear() for dates after 2000, while webkit and Firefox stick with the old behavior

This outputs 99 in all browsers:

javascript:alert(new Date(917823600000).getYear());

This outputs 108 in FF/WebKit, and 2008 in Opera/IE:

javascript:alert(new Date().getYear());

It's dumb. It dates to pre-Y2K days, and now just returns the number of years since 1900 for legacy reasons. Use getFullYear() to get the actual year.


I am using date.getUTCFullYear(); working without problems.


The number you get is the number of years since 1900. Don't ask me why..


As others have said, it returns the number of years since 1900. The reason why it does that is that when JavaScript was invented in the mid-90s, that behaviour was both convenient and consistent with date-time APIs in other languages. Particularly C. And, of course, once the API was established they couldn't change it for backwards compatibility reasons.


BTW, different browsers might return different results, so it's better to skip this function altogether and and use getFullYear() always.


var date_object=new Date(); var year = date_object.getYear(); if(year < 2000) { year = year + 1900; } //u will get the full year ....


it is returning 4 digit year - 1900, which may have been cool 9+ years ago, but is pretty retarded now. Java's java.util.Date also does this.

참고URL : https://stackoverflow.com/questions/98124/why-does-javascript-getyear-return-108

반응형