2016-07-30 52 views
0

從日期選擇日期選擇日期dd-mm-yy格式後,我需要下一年日期-1天day..ie來自:29-07- 2016再到:28-07-2016像這樣...請guyz幫助me..im分享我的源代碼從datepicker中選擇日期後,我希望明年的日期在下一個領域

$('#oldDate').on('change', function(e){ 
 
    var oldDate = new Date(this.value);  
 
    $('.datepicker').datepicker({dateFormat:'dd-mm-yy'}); 
 
    
 
    
 
    
 
    $('#old').html(new Date(oldDate)); 
 
    oldDate.setDate(oldDate.getDate()-1); 
 
    oldDate.setFullYear(oldDate.getFullYear()+1); 
 

 
\t var day = ("0" + oldDate.getDate()).slice(-2); 
 
     var month = ("0" + (oldDate.getMonth() + 1)).slice(-2); 
 

 
     var today = oldDate.getFullYear()+"-"+(month)+"-"+(day); 
 
     
 
//  var today = oldDate.(day)+"-"+(month)+"-"+getFullYear(); 
 
//  alert(today); 
 
     $('#new').val(today);
<p class="left"> 
 
      <label for="from">FROM:</label> 
 
      <input type="text" name="from" id="oldDate" class="datepicker"/> 
 
      </p> 
 
      <p class="pull-right"> 
 
      <label for="to">TO:</label> 
 
      <input type="text" name="to" id="new"> 
 
      </p>

回答

1

爲了達到預期的效果,使用日期選擇器,然後改變使用功能使工作

JS:

$('.datepicker').datepicker({ 
    dateFormat: 'dd-mm-yy' 
}).datepicker("setDate", "0"); 

$('#oldDate').click(function() { 
    $('#oldDate').datepicker('setDate', new Date()); 
}); 

$('#oldDate').on('change', function(e) { 

    console.log(this.value) 
    var x = this.value; 
    var from = x.split("-"); 
    var f = new Date(from[2], from[1] - 1, from[0]); 
    var oldDate = new Date(f); 
    $('#old').html(new Date(oldDate)); 
    oldDate.setDate(oldDate.getDate() - 1); 
    oldDate.setFullYear(oldDate.getFullYear() + 1); 
    console.log(oldDate) 

    var day = ("0" + oldDate.getDate()).slice(-2); 
    var month = ("0" + (oldDate.getMonth() + 1)).slice(-2); 

    var today = (day) + "-" + (month) + "-" + oldDate.getFullYear(); 

    //  var today = oldDate.(day)+"-"+(month)+"-"+getFullYear(); 
    //  alert(today); 
    $('#new').val(today); 
}); 

Codepen- http://codepen.io/nagasai/pen/XKBZmN

+0

它給予'NAN-AN-aN' – Drone

+0

請檢查更新的代碼筆 - http://codepen.io/nagasai/pen/dXjVbJ,我今天看到的格式設置不同 –

+0

請檢查更新後的codepen格式 - mm/dd/yyyy - http://codepen.io/nagasai/pen/Eypwxk –

0

檢查:

<script type="text/javascript"> 
    var dt = new Date('2016/12/10'); 
    var month = dt.getMonth()+1; 
    var day = dt.getDate()-1; 
    var year = dt.getFullYear()+1; 
    alert(month + '-' + day + '-' + year); 
    </script> 
相關問題