JavaScript의 Date 생성자에서 월 인수 범위가 0에서 11까지 인 이유는 무엇입니까?
Date
아래 호출을 사용하여 JavaScript에서 새 개체를 초기화 할 때 월 인수가 0부터 시작한다는 것을 알았습니다.
new Date(2010, 3, 1); // that's the 1st April 2010!
월 인수가 0에서 시작하는 이유는 무엇입니까? 반면, 날짜 인수 (마지막 인수)는 1부터 31까지의 숫자입니다. 그럴만 한 이유가 있습니까?
프로그래밍 세계에서 오래된 (아마도 안타깝게도 죽어가는) 전통입니다. 오래된 표준 (POSIX) localtime C 함수 http://linux.die.net/man/3/localtime 참조
이 질문에 대한 진정한 대답은이 문제가있는에서 복사되었다는 것 java.util.Date
입니다. 증명은 원래 JavaScript ( Date
객체 포함)를 구현 한 사람인 Brendan Eich의 Twitter에서 찾을 수 있습니다 .
https://twitter.com/BrendanEich/status/481939099138654209
https://twitter.com/BrendanEich/status/771006397886533632
이것은 1995 년에 일어 났고 JDK 1.0은 베타 버전이었습니다. 1996 년에 출시되었습니다. 1997 년에 JDK 1.1이 나왔습니다.이 버전은에서 대부분의 함수를 사용하지 않고으로 java.util.Date
이동 java.util.Calendar
했지만 여전히 0부터 시작하는 달이있었습니다. 이에 만족 한 개발자들은 Joda-Time 라이브러리를 만들었으며 궁극적으로 java.time
Java 8 (2014)에 포함 된 패키지로 이어졌습니다 .
In short, it took 18 years for Java to get a correctly designed date/time API built-in, but JavaScript is still stuck back in the dark ages. We do indeed have excellent libraries like Moment.js, date-fns, and js-joda. But as of now, there is nothing more than Date
built-in to the language. Hopefully this will change in the near future.
Everything but the day of the month is 0 based, see here for a full list including ranges :)
It's actually the 1 based days that are the oddballs here...oddly enough. Why was this was done? I don't know...but probably happened the same meeting they got plastered and decided semicolons were optional.
There are always 12 months in a year, so early C implementations might have used a static fixed-width array with indexes 0..11.
Its like this in java too.. Probably to convert int to string (0 - jan,, 1-feb), they coded this way.. because they might have an array of string (indexed from 0) of month names and these month numbers if they start from 0, it'll be lot easier to map to the month strings..
I know it's not really an answer to the original question, but I just wanted to show you my preferred solution to this problem, which I never seem to memorize as it pops up from time to time.
The small function zerofill does the trick filling the zeroes where needed, and the month is just +1
added:
function zerofill(i) {
return (i < 10 ? '0' : '') + i;
}
function getDateString() {
const date = new Date();
const year = date.getFullYear();
const month = zerofill(date.getMonth()+1);
const day = zerofill(date.getDate());
return year + '-' + month + '-' + day;
}
But yes, Date has a pretty unintuitive API, I was laughing when I read Brendan Eich's Twitter.
They might've considered months to be an enumeration (first index being 0) and days not since they don't have a name associated with them.
Or rather, they thought the number of the day was the actual representation of the day (the same way months are represented as numbers in a date like 12/31), as if you could make a enumeration with numbers as the variables, but actually 0-based.
So actually, for the months, perhaps they thought the proper enumeration representation would be to use the month's name, instead of numbers, and they would've done the same if days had a name representation. Imagine if we'd say January Five, January Sixth, instead of January 5, January 6, etc., then perhaps they'd have made a 0-based enumeration for days too...
Perhaps subconsciously they thought about an enumeration for months as {January, February, ...} and for days as {One, Two, Three, ...}, except for days you access the day as a number rather than the name, like 1 for One, etc., so impossible to start at 0...
It might be a flaw, but it's also very handy when you want to represent the months or day of the week as a string you can just create an array like ['jan,'feb' ...etc][new Date().getMonth()] in stead of ['','jan',feb ...etc][new Date().getMonth()] or ['jan','feb' ...etc][new Date().getMonth()-1]
days of the month are normaly not named so you won't be making arrays with names for those. In this case 1-31 is easier to handle, so you want have to subtract 1 every time...
'development' 카테고리의 다른 글
Java의 가비지 수집기는 무엇입니까? (0) | 2020.08.20 |
---|---|
Eclipse : 전체 작업 공간을 어떻게 새로 고치나요? (0) | 2020.08.20 |
PHP 객체를 JSON으로 직렬화 (0) | 2020.08.20 |
Maven 2에서 가능한 모든 목표를 나열 하시겠습니까? (0) | 2020.08.20 |
Nodejs는 응답으로 파일을 보냅니다. (0) | 2020.08.20 |