通过日期JS

时间:2018-10-20 14:38:53

标签: javascript arrays date date-range

我有一个数组

[
  {
    "Date": "01/01/2016",
    "Compliant": 143,
  },
  {
    "Date": "02/01/2016",
    "Compliant": 2016,

  },
  {
    "Date": "03/01/2016",
    "Compliant": 71,
  },
  etc..
  {
    "Date": "11/01/2018",
    "Compliant": 128,
  },
  {
    "Date": "12/01/2018",
    "Compliant": 98,
  }
]

并有两个datePickers DatePickers

<input type="text" id="datepicker-from" class="ui-widget">
<input type="text" id="datepicker-to" class="ui-widget">

我所需要的-是获取数据的总和[i]。符合范围,我将在datePickers中设置 但是,当我设置了所需的范围时-它的日期错误。 我需要在我的JS中修复什么?

$.ajax({
  url: 'data/test.json',
  type: 'GET',
  headers: { accept: 'application/json;odata=verbose;' }
})

function cicle(data) {
  $('.date-selector').change(() => {

    function filterRange(data, from, to) {
      const result = [];

      for (i = 0; i < data.length; i++) {
        const date = data[i].Date;
        if (data >= from && date <= to) {
          let t = moment(data[i].Date).format('L');
          
          result.push(data[i].Date);
        }
      }

      return result;
    }
    const start = $('#datepicker-from').datepicker('getDate');
    const end = $('#datepicker-to').datepicker('getDate');
    
    const m = start.getMonth() + 1 >= 10 ? start.getMonth() + 1 : `0${start.getMonth() + 1}`;
    const d = start.getDate() >= 10 ? start.getDate() : `0${start.getDate()}`;
    const yy = start.getFullYear();
    const st = `${m}/${d}/${yy}`; // yyyy-mm-dd

    const mm = end.getMonth() + 1 >= 10 ? end.getMonth() + 1 : `0${end.getMonth() + 1}`;
    const dd = end.getDate() >= 10 ? end.getDate() : `0${end.getDate()}`;
    const yyyy = end.getFullYear();
    const en = `${mm}/${dd}/${yyyy}`; // yyyy-mm-dd

    const filtered = filterRange(data, st, en);


    console.log(filtered);
  });
}

When i set date in range from 1st Jan 2016 to 1st March 2016 it's showing me these months from all Years

0 个答案:

没有答案