用javascript比较日期

时间:2016-02-24 22:50:07

标签: javascript html date comparison calc

我正在尝试对两个日期进行比较,一个是从我的数据库中获取的日期,另一个是用户在HTML输入表单上的数字。 日期格式为DD / MM / YYYY。 我们的用户必须通知字段dPaid_Date上的日期以及它将出现在nPaid_Amount上的结果,它将根据用户支付的日期显示金额...

<form action="PM_CL_Include08.php" name="form" method="POST">
    <input type="hidden" name="nID" value="1">
    <input type="hidden" name="nID_PaymentEntries" value="1">
    <input type="hidden" name="nPg" value="1">
            <tr>
                <td class="content-form" align="left" valign="top">
                    <div class="form-group">
                        <label>Aluno</label>
                        <input value=Isabelle V. D. Beek class="form-control" disabled>                                     </div>
                    <div class="form-group">
                        <label>Modalidade</label>
                        <input value="Ballet na Unidade Jacaré com Gabriella Belchior de Segunda/Quarta 09h00" class="form-control" disabled>                                      </div>
                    <div class="form-group">
                        <label>Ano</label>
                        <input value="2016" class="form-control" disabled>
                    </div>
                    <div class="form-group">
                        <label>Mês</label>
                                                                    <input value="Janeiro" class="form-control" disabled>
                    </div>
                    <div class="form-group">
                        <label>Data do Pagamento</label>
                        <input name="dPaid_Date" id="dDate" class="form-control" onchange="fCalc();">
                    </div>
                    <div class="form-group">
                        <label>Valor do Pagamento</label>
                        <input name="dPaid_Amount" id="nAmount" class="form-control">
                    </div>
                </td>
            </tr>
            <script language="JavaScript">
            function fCalc(){
                var dDate = document.form.dPaid_Date.value;
                var nAmount = 80;
                document.form.nAmount.value = nAmount;
                var dCondition = "07/01/2016";
                if(dDate < dCondition){
                    var nAmount_ = -10;
                    document.form.dPaid_Amount.value = nAmount+nAmount_;
                }
                var dCondition = "15/01/2016";
                if(dDate > dCondition){
                    var nAmount_ = 10;
                    document.form.dPaid_Amount.value = nAmount+nAmount_;
                }
            }
            </script>
        <tr>
        <td class="content-button" align="center" valign="middle">
            <button class="button"><i class="fa fa-angle-left"></i> ENVIAR <i class="fa fa-angle-right"></i></button>
        </td>
    </tr>
</form>

我找到了一种方法:

<form action="PM_CL_Edit02.php" name="form" method="POST" onsubmit="return fValidate(this);">
    <input type="hidden" name="nYear" value="2016">
    <input type="hidden" name="nMonth" value="1">
    <input type="hidden" name="nID_PaymentEntries" value="1">
    <input type="hidden" name="nPg" value="1">
    <tr>
        <td class="content-form" align="left" valign="top">
            <div class="form-group">
                <label>Aluno</label>
                <input value=Isabelle V. D. Beek class="form-control" disabled>                                     </div>
            <div class="form-group">
                <label>Modalidade</label>
                <input value="Ballet na Unidade Jacaré com Gabriella Belchior de Segunda/Quarta 09h00" class="form-control" disabled>                                      </div>
            <div class="form-group">
                <label>Ano</label>
                <input value="2016" class="form-control" disabled>
            </div>
            <div class="form-group">
                <label>Mês</label>
                                                            <input value="Janeiro" class="form-control" disabled>
            </div>
            <div class="form-group">
                <label>Data do Pagamento <a onclick="fCalc();"><i class="fa fa-refresh"></i></a></label> 
                <input name="dPaid_Date" value="05/01/2016" id="dDate" class="form-control" onchange="fCalc();">
            </div>
            <div class="form-group">
                <label>Valor do Pagamento</label>
                <input name="nPaid_Amount" value="70" id="nAmount" class="form-control">
            </div>
        </td>
    </tr>
    <script language="JavaScript">
    function fCalc(){
        var dDate = document.form.dPaid_Date.value;
        var dDate_ = dDate.split(/\D/);
        var dDate_Day = dDate_[0];
        var dDate_Month = dDate_[1]-1;
        var dDate_Year = dDate_[2];
        var dDate = new Date(dDate_Year, dDate_Month, dDate_Day);
        var nAmount = 80;
        document.form.nAmount.value = nAmount;
        var dCondition = "07/01/2016";
        var dCondition_ = dCondition.split(/\D/);
        var dCondition_Day = dCondition_[0];
        var dCondition_Month = dCondition_[1]-1;
        var dCondition_Year = dCondition_[2];
        var dCondition = new Date(dCondition_Year, dCondition_Month, dCondition_Day);
        if(dDate < dCondition){
            var nAmount_ = -10;
            document.form.nPaid_Amount.value = nAmount+nAmount_;
        }
        var dCondition = "15/01/2016";
        var dCondition_ = dCondition.split(/\D/);
        var dCondition_Day = dCondition_[0];
        var dCondition_Month = dCondition_[1]-1;
        var dCondition_Year = dCondition_[2];
        var dCondition = new Date(dCondition_Year, dCondition_Month, dCondition_Day);
        if(dDate > dCondition){
            var nAmount_ = 10;
            document.form.nPaid_Amount.value = nAmount+nAmount_;
        }
    }
    </script>
    <tr>
        <td class="content-button" align="center" valign="middle">
            <button class="button"><i class="fa fa-angle-left"></i> ENVIAR <i class="fa fa-angle-right"></i></button>
        </td>
    </tr>
</form>

1 个答案:

答案 0 :(得分:0)

将它们转换为Date对象,然后进行比较。

http://www.w3schools.com/js/js_date_methods.asp

var testdate1 = Date.parse("11/30/2011");
var testdate2 = Date.parse("12/30/2012");

// Can use relational operators < <= > >= for dates

testdate1 <  testdate2; // true
testdate1 <= testdate2; // true
testdate1 >  testdate2; // false
testdate1 >= testdate2; // false

抱歉,我的约会&#39;上课没有补充。无论如何,更新的方法更快。