Javascript 取得每月分別有幾天本月(某月)的天數

5 月 4, 2018 | | 0 條留言

使用方法

var curMonthDays = new Date(year,month,0).getDate();

範例展示

Click the button to display 此月份有幾天.
<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
    function myFunction() {
        var d = new Date(2018,1,0) //取得2018/01月份有幾天
        var n = d.getDate();
        document.getElementById(demo).innerHTML = n;
    }
</script>

//輸出 31

Demo

See the Pen
How many days in a month ues Javascript
by Leon Cheng (@jq153387)
on CodePen.