揮発性のメモ2

http://d.hatena.ne.jp/iww/

日付を入力するときの月末日 計算器



日 (1~)

翌月の0日=今月の末日 という話

/** 現在日時をセットして、月末計算器もついでにセット */
function initLastdate(){
    /** 月末計算器 */
    function setLastdate(){
        const year  = $("#cbyear").val();
        const month = $("#cbmonth").val();
        const tmpdate = new Date(year,month,0); // 翌月の0日=今月の末日
        const lastdate = tmpdate.getDate();
        $("#cbdate").prop("max",lastdate); // 日入力欄の最大値を月末日に設定
        $("#cblastdate").text(lastdate);
    }

    const now = new Date();
    $("#cbyear").val(now.getFullYear()).on("change",setLastdate);
    $("#cbmonth").val(now.getMonth()+1).on("change",setLastdate);
    $("#cbdate").val(now.getDate());
    setLastdate();
}
$(initLastdate);