当用户在文本框中输入日期时,如何计算一年的持续时间

how to calculate the duration of year when user entered the date in textbox

本文关键字:计算 持续时间 一年 何计算 文本 用户 输入 日期      更新时间:2023-09-26

如何显示持续时间?在文本框中输入日期时,在下一个字段中显示总年数(持续时间)。例:如果用户输入 09/11/2015.它应该显示为 0.5 作为值,通过与今天的日期进行比较。'现在我使用脚本脚本

function cal()
{
   obj = document.getElementById("Date");
        if (obj != null) {
            if (obj.value != "") {
                var year = obj.value.split("/")[2];
                var today = new Date();
                if (year > today.getFullYear()) {
                    alert("Invalid Year");
                    document.getElementById("Date").value = "";
                    document.getElementById("year").value = "";
                }
                else {
               
         document .getElementById("year").value = today.getFullYear() - year;
               } }
            }
}

'

function aa(){
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2016,04,12);
var secondDate = new Date(2016,04,25);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
alert(diffDays+" Day(s)");
//document.getElementById('test').innerHTML=diffDays;
}
<button onClick="aa()">
Click
</button>