반응형
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
반응형
'itsource' 카테고리의 다른 글
PHP-FPM 및 Nginx: 502 불량 게이트웨이 (0) | 2022.12.04 |
---|---|
프래그먼트 안에 소프트 키보드를 숨기는 방법 (0) | 2022.12.04 |
임베디드 MongoDB(연동 테스트 실행 시) (0) | 2022.12.04 |
오류: vcvarsall.bat을 찾을 수 없습니다. (0) | 2022.12.04 |
vue.flash 워치가 업데이트되지 않았습니다. (0) | 2022.12.04 |