itsource

JavaScript를 사용하여 지정된 달의 일수를 가져오시겠습니까?

mycopycode 2022. 12. 4. 22:37
반응형

JavaScript를 사용하여 지정된 달의 일수를 가져오시겠습니까?

중복 가능성:
javascript를 사용하여 한 달 중 일수를 결정하는 가장 좋은 방법은 무엇입니까?

월이 숫자와 년으로 표시된다고 합니다.

// Month in JavaScript is 0-indexed (January is 0, February is 1, etc), 
// but by using 0 as the day it will give us the last day of the prior
// month. So passing in 1 as the month number will return the last day
// of January, not February
function daysInMonth (month, year) {
    return new Date(year, month, 0).getDate();
}

// July
daysInMonth(7,2009); // 31
// February
daysInMonth(2,2009); // 28
daysInMonth(2,2008); // 29
Date.prototype.monthDays= function(){
    var d= new Date(this.getFullYear(), this.getMonth()+1, 0);
    return d.getDate();
}

다음은 유효한 날짜/시간 값을 가져와서 연결된 달의 일 수를 반환합니다.다른 두 가지 답변의 애매함을 제거한다...

 // pass in any date as parameter anyDateInMonth
function daysInMonth(anyDateInMonth) {
    return new Date(anyDateInMonth.getFullYear(), 
                    anyDateInMonth.getMonth()+1, 
                    0).getDate();}

다른 가능한 옵션은 Datejs를 사용하는 것입니다.

그럼 넌 할 수 있어

Date.getDaysInMonth(2009, 9)     

이 기능만을 위해 라이브러리를 추가하는 것은 무리입니다만, 이용 가능한 모든 옵션을 알고 있는 것은 항상 좋은 일입니다.

언급URL : https://stackoverflow.com/questions/1184334/get-number-days-in-a-specified-month-using-javascript

반응형