2

JavaScriptのオブジェクトの配列から特定の日付範囲の間のデータを取得するためにalasqlを使用しています。 以下のクエリを試しましたが、結果が得られません。私は開始日終了日を渡しています。これらの範囲の間のデータのみが必要です。これを行う他の方法があれば、それも役に立ちます。これを行うのを手伝ってください、事前に感謝します

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]);
4

2 に答える 2

2

これを試して:

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>

于 2016-04-05T12:38:06.097 に答える
0

これは、 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
}

于 2016-04-05T11:35:15.570 に答える