簡體   English   中英

日期添加公式javascript

[英]Date addition formula javascript

我正在使用php開發發票生成程序。 在其中,我使用datetimepicker來使用發票生成日期和到期日。 現在,我希望通過向發票生成日期字段添加特定值來自動填寫截止日期字段。 以下是我的html:

<div class = "col-lg-4">
                <select id = "invoice_duration"  class="form-control input-group placeholder">
                    <option>10</option>
                    <option>20</option>
                    <option>30</option>
                    <option>40</option>
                    <option>50</option>
                    <option>50</option>
                </select>
                </div><br><br>

                <div class = "col-lg-6">
                    <h4> Invoice Date </h4>
                     <div class="input-append date datetimepicker">
                      <input id = "invoice_date" style = "height : 35px;" style = "height : 30px;" style = "height : 30px;"  type="text" name = "invoice_date">
                      <span style = "height : 35px;" class="add-on">
                        <i style = "padding-top:5px;" data-time-icon="fa fa-time" data-date-icon="fa fa-calendar"></i>
                      </span>
                    </div>                      
                </div>


                <div class = "col-lg-6">
                <h4> Due Date </h4>
                    <div class="input-append date datetimepicker">
                        <input id = "due_date" style = "height : 35px;" style = "height : 30px;"  type="text" name = "due_date">
                        <span style = "height : 35px;" class="add-on">
                            <i style = "padding-top:5px;" data-time-icon="fa fa-time" data-date-icon="fa fa-calendar"></i>
                        </span>
                    </div>
                </div>

我的腳本如下:

<script>
    $( "#due_date" ).click(function() {

        var invoice_date = $("#invoice_date").val();
        var invoice_duration = $("#invoice_duration").val();

        var splited_invoice_date = invoice_date.split("/");
        var day = splited_invoice_date[0];
        var month = splited_invoice_date[1];
        var year = splited_invoice_date[2];
        var total_days = (+year_days) + (+month_days) + (+days);
        alert('day :' +day+ 'Month :' +month+ 'Year :' +year);

        });

使用此工具,我可以成功獲取警報,並給我隔離的日期。 現在,可以輕松地從“選擇”字段中添加值並將其添加到invoice_generation_date中,以最終將某些內容放入Due_date字段中。

基本上我想要一個日期加法公式。 對不起,如果我模棱兩可。

提前致謝。

我會在更改前一個字段而不是輸入click時觸發事件。

在js中添加日期的最簡單方法是每天使用幾秒鍾,然后使用格式原語。

這樣的事情應該起作用:

$("#invoice_date").change(function(){
   var invoice_date = $("#invoice_date").val();
   var invoice_duration = parseInt($("#invoice_duration").val(),10);

   var d = new Date(invoice_date).getTime()+(invoice_duration*24*60*60*1000);
   d = new Date(d);
   var dFormatted =  d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear();

   $("#due_date").val(dFormatted).trigger('click');
 });

最后的點擊觸發器將打開校准彈出窗口。 如果您不希望它自動發生,可以將其刪除。

更簡單的提琴: http : //jsfiddle.net/16gov1ms/1/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM