2017-07-08 112 views
-1
I have one dropdown and two textbox on my page. Now i have to bind the value 
based on dropdown value. 

My question is when i am select This Week from dropdown that time it will 
display wrong date when first date of month on second textbox. 

看看下面的例子。它在月的其他日期工作正常,但當 選擇'07/01/2017'那麼它的顯示是這樣的'01/06/2017'而不是 '01/07/2017'第二個文本框,當我們選擇本週日期篩選不適用於每月的第一個月

$(document).on("change","#selectDates",function() { 
 
      var dropValue = document.getElementById('selectDates').value; 
 
      //All 
 
      if (dropValue == "1") { 
 
       $('#txtDateFrom').val(''); 
 
       $('#txtDateTo').val(''); 
 
      } 
 
       //Today 
 
      else if (dropValue == "2") { 
 
       var back_GTM = new Date(); 
 
       $('#txtDateFrom').val(Back_date(back_GTM)); 
 
       $('#txtDateTo').val(Back_date(back_GTM)); 
 
       
 
      } 
 
       //This Week 
 
      else if (dropValue == "3") { 
 
       //var curr = new Date; // get current date 
 
       var curr = new Date('07/01/2017'); // get current date 
 
       var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week 
 
       var last = first + 6; // last day is the first day + 6 
 

 
       var firstday = new Date(curr.setDate(first)); 
 
       var lastday = new Date(curr.setDate(last)); 
 

 
       $('#txtDateFrom').val(Back_date(firstday)); 
 
       $('#txtDateTo').val(Back_date(lastday)); 
 
       
 
      } 
 
      }); 
 
      
 
      function Back_date(back_GTM) { 
 
      var b_dd = back_GTM.getDate(); 
 
      var b_mm = back_GTM.getMonth() + 1; 
 
      var b_yyyy = back_GTM.getFullYear(); 
 
      if (b_dd < 10) { 
 
       b_dd = '0' + b_dd 
 
      } 
 
      if (b_mm < 10) { 
 
       b_mm = '0' + b_mm 
 
      } 
 
      return back_date = b_dd + '/' + b_mm + '/' + b_yyyy; 
 
     }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="select-style"> 
 
       <select id="selectDates"> 
 
        <option value="1" selected>All</option> 
 
        <option value="2">Today</option> 
 
        <option value="3">This Week</option> 
 
        </select> 
 
      </div> 
 
      <br />   <br /> 
 

 
      <div class="input-group input-large" data-date="13/07/2013" data-date-format="dd/mm/yyyy"> 
 
       <span class="input-group-addon">From</span> 
 
       <input type="text" id="txtDateFrom" class="form-control dpd1" name="from"> 
 
       <span class="input-group-addon">To</span> 
 
       <input type="text" id="txtDateTo" class="form-control dpd2" name="to"> 
 
      </div>

回答

1

您應該創建其中每週重疊2個月的情況下兩個日期的對象。

因爲當您將日期設置爲-5時,從2017年7月1日起,它會將日期正確計算到星期日6月25日。
但現在,這個月已經改變了!

當您將日期設置爲星期的最後一天(即1)時,月份保持在日期對象中的六月。

因此,有兩個不同的日期對象分別處理日期是修復。

$(document).on("change","#selectDates",function() { 
 
    var dropValue = document.getElementById('selectDates').value; 
 
    //All 
 
    if (dropValue == "1") { 
 
    $('#txtDateFrom').val(''); 
 
    $('#txtDateTo').val(''); 
 
    } 
 
    //Today 
 
    else if (dropValue == "2") { 
 
    var back_GTM = new Date(); 
 
    $('#txtDateFrom').val(Back_date(back_GTM)); 
 
    $('#txtDateTo').val(Back_date(back_GTM)); 
 

 
    } 
 
    //This Week 
 
    else if (dropValue == "3") { 
 
    //var curr = new Date; // get current date 
 
    var curr = new Date('07/01/2017'); // get current date 
 
    var curr2 = new Date('07/01/2017'); // get current date - Second date object. 
 
    
 
    var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week 
 
    var last = first + 6; // last day is the first day + 6 
 

 
    var firstday = new Date(curr.setDate(first)); 
 
    var lastday = new Date(curr2.setDate(last)); 
 

 
    $('#txtDateFrom').val(Back_date(firstday)); 
 
    $('#txtDateTo').val(Back_date(lastday)); 
 

 
    } 
 
}); 
 

 
function Back_date(back_GTM) { 
 
    var b_dd = back_GTM.getDate(); 
 
    var b_mm = back_GTM.getMonth() + 1; 
 
    var b_yyyy = back_GTM.getFullYear(); 
 
    if (b_dd < 10) { 
 
    b_dd = '0' + b_dd 
 
    } 
 
    if (b_mm < 10) { 
 
    b_mm = '0' + b_mm 
 
    } 
 
    return back_date = b_dd + '/' + b_mm + '/' + b_yyyy; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="select-style"> 
 
    <select id="selectDates"> 
 
    <option value="1" selected>All</option> 
 
    <option value="2">Today</option> 
 
    <option value="3">This Week</option> 
 
    </select> 
 
</div> 
 
<br />   <br /> 
 

 
<div class="input-group input-large" data-date="13/07/2013" data-date-format="dd/mm/yyyy"> 
 
    <span class="input-group-addon">From</span> 
 
    <input type="text" id="txtDateFrom" class="form-control dpd1" name="from"> 
 
    <span class="input-group-addon">To</span> 
 
    <input type="text" id="txtDateTo" class="form-control dpd2" name="to"> 
 
</div>

+0

謝謝它節省了我的時間.. –

+0

我回到了我的第一個想法...使用兩個日期對象。因爲設置第一天前的最後一天不會在月底附近解決同一問題。如果你設置「06/28/2017」...結果是從2017年7月25日到2017年7月1日,這是錯誤的。 –

+0

Yup.it顯示錯誤的日期。 –

0

爲了創建新的日期,你應該通過每月爲prameters年,月指數和日期。 舉例如下:

var curr = new Date(2017,6,1); 

按照需要被固定在你的代碼:

// Needs to fix this 
var curr = new Date('07/01/2017'); 
+0

感謝重播..! –