我有一个数组
[
{
"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);
});
}