我正在使用 alasql 在JavaScript中对象数组的给定日期范围之间获取数据。 我试过下面的查询,但我没有得到任何结果。我正在通过开始日期和结束日期,我只希望数据这些范围,如果还有其他方法可以执行此操作也会帮助我。请帮我这样做, 提前谢谢
startTime = Sun Mar 27 2016 00:00:00 GMT+0530 (India Standard Time)
endTime = Sat Apr 02 2016 00:00:00 GMT+0530 (India Standard Time)
data= [
{
name:'Sam',
reportDate:'3/25/2016 16:34:00 PM'
},{
name:'Mac',
reportDate:'3/27/2016 10:26:00 PM'
},{
name:'Sandy',
reportDate:'3/30/2016 19:14:00 AM'
},{
name:'Smith',
reportDate:'4/2/2016 14:54:00 PM'
},{
name:'Warner',
reportDate:'4/4/2016 17:04:00 AM'
},{
name:'Finch',
reportDate:'3/28/2016 11:20:00 PM'
}
]
alasql("SELECT * from ? WHERE DATE([[0].reportDate]) >=" + startDate + "AND DATE([[0].reportDate]) <= " + endDate + "", [data]);
答案 0 :(得分:1)
您可以通过 javascript 抓取数据:
(仅当startTime
和endTime
为Strings
时才有效。)
var month = ["Jan", "Feb", "Mar", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"];
startTime = "Sun Mar 27 2016 00:00:00 GMT+0530 (India Standard Time)"
endTime = "Sat Apr 02 2016 00:00:00 GMT+0530 (India Standard Time)"
data = [{
name: 'Sam',
reportDate: '3/25/2016 16:34:00 PM'
}, {
name: 'Mac',
reportDate: '3/27/2016 10:26:00 PM'
}, {
name: 'Sandy',
reportDate: '3/30/2016 19:14:00 AM'
}, {
name: 'Smith',
reportDate: '4/2/2016 14:54:00 PM'
}, {
name: 'Warner',
reportDate: '4/4/2016 17:04:00 AM'
}, {
name: 'Finch',
reportDate: '3/28/2016 11:20:00 PM'
}]
// get the startTime f.e. 3/27/2016
var startT = /\s(\w+)\s(\d+)\s(\d+)/.exec(startTime); // get month out of startTime
var mS = month.indexOf(startT[1]) + 1; // get month & exchange month into digit
var dS = /0?(\d+)/.exec(startT[2])[1]; // get rid of possible "0" from day
var yS = startT[3];
var startTime_ = mS + "/" + dS + "/" + yS;
// get the endTime
var endT = /\s(\w+)\s(\d+)\s(\d+)/.exec(endTime); // get month out of startTime
var mE = month.indexOf(endT[1]) + 1; // get month & exchange month into digit
var dE = /0?(\d+)/.exec(endT[2])[1]; // get rid of possible "0" from day
var yE = endT[3];
var endTime_ = mE + "/" + dE + "/" + yE;
var startIndex = 0;
var endIndex = 0;
// get startIndex from data-array
data.forEach(function(e, i) {
if (e.reportDate.indexOf(startTime_) >= 0) {
startIndex = i; // this is where we start to get data from date-array
}
})
// get endIndex from data-array
data.forEach(function(e, i) {
if (e.reportDate.indexOf(endTime_) >= 0) {
endIndex = i; // this is where we end to get data from date-array
}
})
for (var i=startIndex; i<=endIndex; i++){
document.write("name: "+data[i].name+"<br/>reportDate: "+data[i].reportDate+"<br/><br/>"); //data[i] is the data you want
}
答案 1 :(得分:1)
试试这个:
var startDate = '2016-03-27';
var endDate = '2016-04-02';
var data = [
{
name:'Sam',
reportDate:'3/25/2016 16:34:00 PM'
},{
name:'Mac',
reportDate:'3/27/2016 10:26:00 PM'
},{
name:'Sandy',
reportDate:'3/30/2016 07:14:00 AM'
},{
name:'Smith',
reportDate:'4/2/2016 02:54:00 PM'
},{
name:'Warner',
reportDate:'4/4/2016 05:04:00 PM'
},{
name:'Finch',
reportDate:'3/28/2016 11:20:00 PM'
}
];
var result = alasql("SELECT * from ? WHERE DATE(reportDate) >= DATE('" + startDate +"') AND DATE(reportDate) <= DATE('" + endDate + "')", [data]);
document.body.innerHTML = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/alasql/0.2.5/alasql.min.js"></script>