创建一个文本框,在一个文本框中,让用户输入他的出生月份,第二个文本框中输入月份中的日期,编写一个JavaScript程序,在第三个文本框中打印距离用户生日还有多少天。
<div id="box"><label for="">请输入您的出生月份:</label> <input type="text"><label for="">请输入您的出生日期:</label> <input type="text"><button>确定</button><label for="">距离您的生日还有:</label> <input type="text"></div><script>var btn = document.querySelector('button');console.log(btn);btn.onclick = function () {var time1 = new Date();var year = time1.getFullYear();console.log(year);var ins = document.querySelectorAll('input');console.log(ins);var m2 = ins[0].value;var d2 = ins[1].value;console.log(m2, d2);var time2 = new Date(`${year}/${m2}/${d2} 12:00:00`);console.log(time2);var time3 = time2.getTime() - time1.getTime();console.log(time3);if (time3 < 0) {year = year + 1;console.log(year);var time2 = new Date(`${year}/${m2}/${d2} 12:00:00`);var time3 = time2.getTime() - time1.getTime();}console.log(time3);var days = Math.floor(time3 / (1000 * 60 * 60 * 24));console.log(days);ins[2].value = days;}</script>