打印输入

时间:2014-10-22 11:12:38

标签: javascript html forms

当用户输入生日日期时,我有一个问题需要计算,以查看下一个生日的剩余时间以及结果是否写入输入。这是代码:

<script type="text/javascript">
  function calcform(){
    with (document.form1) {
    var today = new Date();
    var fullyear = today.getFullYear();
    var future = new Date(+number1.value+ fullyear);
    var diff = future.getTime() - today.getTime();
    var days = Math.floor(diff / (1000 * 60 * 60 * 24 ));
    }
  }
</script>
<form name="form1" action="">
    Type your date birth:
    <input type="text" id="number1" value="" size="30" /><br/>
    <p>
    Number of days until your birthday:
    <input type="text" id="result" value="" size="13" /><br/>
    <p>
    <input type="button" id="Calculate" value="Calculate" onclick="calcform()" />
    <input type="reset" value="Clear"/>
</form>

1 个答案:

答案 0 :(得分:0)

修正了它,你需要增加日期,直到下一个生日大于当前日期,然后测量天数。

function calcform(){
    var today = new Date();
    var fullyear = today.getFullYear();
    var future = new Date(document.getElementById('number1').value);
    future.setFullYear(fullyear);
    if(future.getTime()<today.getTime()) future.setFullYear(fullyear+1);
    var diff = future.getTime() - today.getTime();
    var days = Math.floor(diff / (1000 * 60 * 60 * 24 ));
    document.getElementById('result').value=days;
}