计算天数

时间:2015-04-21 10:32:12

标签: javascript html

javascript代码无法运行。似乎无法在代码中找到任何错误。必须是一些小错误。我在这里检查过osts的逻辑。

<html>
    <head>
        <title></title>
        <script>
        function finddays()
        {
            var today = new Date();
            var user = new Date(myform.year.value,myform.month.value,myform.day.value,0,0,0,0);
            var msPerDay = 24 * 60 * 60 * 1000;
            var daysLeft = Math.abs((user.getTime() - today.getTime())/msPerDay);
            daysLeft = Math.round(daysLeft);
            retun(daysLeft);
        }
        </script>
    </head>
    <body>
        <form name="myform">
            <input type="number" name="date" value="date" min="1" max="31">
            <input type="number" name="month" value="month" min="1" max="12">
            <input type="number" name="year" value="year" min="1980" max="2200">
            <input type="submit" name="submit" value="SUBMIT" onclick="windows.alert ('Number of days: '+ finddays())">
        </form>
    </body>
</html>

3 个答案:

答案 0 :(得分:1)

@Riya Tilwani

您缺少以下内容:

  • 您有html元素名称日期,但您已尝试使用检索它。请将其更改为以下内容:
    new Date(myform.year.value,myform.month.value,myform.date.value,0,0,0,0)
  • retun(daysLeft)语法错误。请将其更改为以下内容:
    return(daysLeft);
  • 在HTML中使用onclick功能时,您已使用 windows.alert 。而是使用以下
    window.alert
  • 此外,您计算的时间也不准确,因为月份被视为从0开始的索引。因此,请使用以下内容(将月份减少1):
    var user = new Date(myform.year.value,myform.month.value-1,myform.date.value,0,0,0,0);
    我已经测试了代码,如果您进行了上述更改,则可以正常运行。

  • 答案 1 :(得分:0)

    这样可以,只需复制粘贴完整的代码。

    <html>
        <head>
            <title></title>
            <script>
            function finddays()
            {
                var today = new Date();
                var user = new Date(myform.year.value,myform.month.value,myform.date.value,0,0,0,0);
                var msPerDay = 24 * 60 * 60 * 1000;
                var daysLeft = Math.abs((user.getTime() - today.getTime())/msPerDay);
                daysLeft = Math.round(daysLeft);
                alert('Number of days: '+daysLeft);
            }
            </script>
        </head>
        <body>
            <form name="myform">
                <input type="number" name="date" value="date" min="1" max="31">
                <input type="number" name="month" value="month" min="1" max="12">
                <input type="number" name="year" value="year" min="1980" max="2200">
                <input type="submit" name="submit" value="SUBMIT" onclick="finddays();">
            </form>
        </body>
    </html>
    

    答案 2 :(得分:0)

    你犯了两个错误:

    1)使用myform而不声明

    2)使用提交按钮代替按钮。

    请将您的代码更改为:

    HTML

    <form name="myform" id="myform">
        <input type="number" name="date" value="date" min="1" max="31"/>
        <input type="number" name="month" value="month" min="1" max="12"/>
        <input type="number" name="year" value="year" min="1980" max="2200"/>
        <input type="button" name="submit" value="SUBMIT" onclick="finddays();"/>
    </form>
    

    Javascript

    function finddays()
    { 
        var myform=document.getElementById("myform");
        var today = new Date();
    
        var user = new Date(myform.year.value,myform.month.value,myform.date.value,0,0,0,0);
        var msPerDay = 24 * 60 * 60 * 1000;
        var daysLeft = Math.abs((user.getTime() - today.getTime())/msPerDay);
        daysLeft = Math.round(daysLeft);
        alert('Number of days: '+daysLeft);
    }
    

    <强> Check out JSFiddle