我有一个带有数据的json文件,
[
[1,622,782,783,"2015-04-21"],
[2,622,782,783,"2015-04-21"],
[3,622,782,783,"2015-04-22"],
[4,622,782,783,"2015-04-23"],
[5,622,782,783,"2015-04-24"],
[6,622,782,783,"2015-04-28"],
[7,622,782,783,"2015-04-28"],
[8,622,782,783,"2015-04-29"],
[9,622,782,783,"2015-05-04"],
[10,622,782,783,"2015-05-05"]
]
如何选择/加载日期2015-04-23
和2015-04-28
之间的值?
答案 0 :(得分:1)
快速选择:
var data = [
[1, 622, 782, 783, "2015-04-21"],
[2, 622, 782, 783, "2015-04-21"],
[3, 622, 782, 783, "2015-04-22"],
[4, 622, 782, 783, "2015-04-23"],
[5, 622, 782, 783, "2015-04-24"],
[6, 622, 782, 783, "2015-04-28"],
[7, 622, 782, 783, "2015-04-28"],
[8, 622, 782, 783, "2015-04-29"],
[9, 622, 782, 783, "2015-05-04"],
[10, 622, 782, 783, "2015-05-05"]
];
var fromTime = new Date("2015-04-23").getTime();
var toTime = new Date("2015-04-28").getTime();
var filteredDates = [];
var row, date;
for (i in data) {
row = data[i];
date = new Date(row[4]);
if (date.getTime() >= fromTime && date.getTime() <= toTime) {
filteredDates.push(row);
}
}
var results = document.querySelector("#results"); // debugging
results.innerHTML = JSON.stringify(filteredDates); // debugging
<div id="results"></div>
答案 1 :(得分:0)
您还可以使用Date.parse()函数返回自1970年1月1日00:00:00 UTC以来的毫秒数。
然后你可以将比较作为比较整数。
$( document ).ready(function() {
var data = [
[1, 622, 782, 783, "2015-04-21"],
[2, 622, 782, 783, "2015-04-21"],
[3, 622, 782, 783, "2015-04-22"],
[4, 622, 782, 783, "2015-04-23"],
[5, 622, 782, 783, "2015-04-24"],
[6, 622, 782, 783, "2015-04-28"],
[7, 622, 782, 783, "2015-04-28"],
[8, 622, 782, 783, "2015-04-29"],
[9, 622, 782, 783, "2015-05-04"],
[10, 622, 782, 783, "2015-05-05"]
];
var date1 = Date.parse("2015-04-23");
var date2 = Date.parse("2015-04-28");
var length = data.length;
var result = [];
for (var i = 0; i< length ; i++){
if(Date.parse(data[i][4])>date1 && Date.parse(data[i][4])<date2){
result.push(data[i]);
}
}
alert(JSON.stringify(result));
});
您可以在以下jsfiddle
中查看