如何在jQuery中添加Date和Integer

时间:2013-06-03 12:33:42

标签: jquery string date salesforce visualforce

大家好,

     <script type="text/javascript">
          function duedatecal(){
            var invoicedate = $(".invoicedate").val();
             var paymentterms = $(".paymentterms").html();

          }
        </script>
        <apex:form>
        Date:
         <Apex:inputtext value="{!dateIn}" id="time" styleclass="invoicedate" onblur="duedatecal();"/> //say suppose date is 06/03/2013
        Payment Days:
           <apex:outputtext value="{!payment.Net__c}" styleclass="paymentterms"  /> //say suppose will get 10 days here
        Due date:
           <apex:outputtext value="{!duedate}" styleclass="duedate"/> // output shld be 16/03/2013
        </apex:form>

每当我更改输入文本(日期)时,都应该通过添加日期+付款日来相应地显示duedate.Help on this pls

3 个答案:

答案 0 :(得分:1)

要完成此任务,您可以:

function duedatecal(){
    var invoicedate = new Date($(".invoicedate").val());
    var paymentterms = $(".paymentterms").html().val();
    var dueDate = new Date();
    dueDate.setDate(invoicedate.getDate()+paymentterms);
    return dueDate;
}

答案 1 :(得分:1)

试试这个:

function duedatecal(){
    var dmy = $(".invoicedate").val().split("/");        
    var invoicedate = new Date(
                   parseInt(dmy[2], 10),
                   parseInt(dmy[0], 10) - 1,
                   parseInt(dmy[1], 10)
                 );

    var paymentterms = $(".paymentterms").text();
    var dueDate = new Date();
    dueDate.setDate(invoicedate.getDate()+parseInt(paymentterms));
    var duedates = dueDate.getDate()+"/"+(dueDate.getMonth()+1)+"/"+dueDate.getFullYear();

    $(".duedate").html(duedates);
}

答案 2 :(得分:1)

您可能需要考虑使用moment.js。它使得在JavaScript中使用日期和时间变得更加容易。

function duedatecal(){

    // parse the input date using a format string
    var invDate = moment($(".invoicedate").val(), 'DD/MM/YYYY');

    // get an integer for the days of payment terms
    var terms = parseInt($(".paymentterms").text());

    // use moment.add with the days parameter
    var dueDate = invDate.add('days', terms);

    // output the due date, specifying a format string
    $(".duedate").text(dueDate.format('DD/MM/YYYY'));
}

这假设您正在对解析输入和格式输出的格式进行硬编码。有关其他选项,请参阅moment.js documentation