{
"id":1,
"name":"Jack",
"date":"01-06-2017"
},
{
"id":2,
"name":"Allen",
"date":"07-08-2017"
},
{
"id":3,
"name":"Annie",
"date":"22-11-2017"
},
存储在'members'数组中的这个JSON我希望根据开始和结束日期过滤成员,如:
let startDate;
let endDate;
let selectedMembers = this.members.filter(m => m.date > startDate && m.date < endDate);
类似的东西..
答案 0 :(得分:1)
首先需要将日期从字符串转换为日期类型,以便正确比较它们,然后执行以下操作:
let members: any[] = [{
"id": 1,
"name": "Jack",
"date": "01-06-2017" // this should be of object Date()
},
{
"id": 2,
"name": "Allen",
"date": "07-08-2017" // this should be of object Date()
},
{
"id": 3,
"name": "Annie",
"date": "22-11-2017" // this should be of object Date()
}];
let start = "01-02-2017; // this should be of object Date()
let end = "06-07-2017"; // this should be of object Date()
let selectedMembers = members.filter(m => {
if ( m.date > start && m.date < end) // or you can cast here to Date()
return m;
});
console.log(selectedMembers);
答案 1 :(得分:1)
要对存储为字符串的日期进行排序,您应该使用基于ISO的格式: https://es.wikipedia.org/wiki/ISO_8601
你写的这行将使用这样的日期:
{ "id":1, "name":"Jack", "date":"2017-06-01" },
{ "id":2, "name":"Allen", "date":"2017-08-07" },
{ "id":3, "name":"Annie", "date":"2017-11-22" }
如果没有,则必须将字符串解析为date对象并在比较中使用它们。像这样:
let startDate = new Date('2017-05-01');
let endDate = new Date('2017-09-01);
let selectedMembers = this.members.filter(m => new Date(m.date) > startDate && new Date(m.date) < endDate);
答案 2 :(得分:1)
您需要撤消日期格式,然后将其转换为 TIMESTAMP 进行比较。
members = [{
"id":1,
"name":"Jack",
"date":"01-06-2017"
},
{
"id":2,
"name":"Allen",
"date":"07-08-2017"
},
{
"id":3,
"name":"Annie",
"date":"22-11-2017"
}];
const startDate ="25-06-2017";
const endDate = "10-11-2017";
// Here reverse the date format & then convert to TIMESTAMP
function reverseAndTimeStamp(dateString) {
const reverse = new Date(dateString.split("-").reverse().join("-"));
return reverse.getTime();
}
// A simple array filter like what you did
const selectedMembers = members.filter(m => {
return reverseAndTimeStamp(m.date) > reverseAndTimeStamp(startDate) && reverseAndTimeStamp(m.date) < reverseAndTimeStamp(endDate)
}
);
console.log(selectedMembers); // the result objects
&#13;
答案 3 :(得分:1)
感谢大家帮助我的答案。这是我的问题的解决方案
{
"id":1,
"name":"Jack",
"date":"01-06-2017"
},
{
"id":2,
"name":"Allen",
"date":"07-08-2017"
},
{
"id":3,
"name":"Annie",
"date":"22-11-2017"
},
let start = "01-02-2017;
let end = "06-07-2017";
let.selectedMembers = this.members.filter(
m => new Date(m.date) >= new Date(startDate) && new Date(m.date) <= new Date(endDate)
);
console.log(selectedMembers);
感谢您的所有答案,这有助于我弄清楚:) ..
答案 4 :(得分:0)
因为你使用法语日期格式,你需要在字符串和Date对象之间建立自己的变换器。
然后你必须处理Date javascript对象和时间戳表示。
这样的事情会起作用。
function parseDate(input) {
var parts = input.match(/(\d+)/g);
// note parts[1]-1
return new Date(parts[2], parts[1]-1, parts[0]).getTime();
}
let startDate = parseDate('01-06-2017');
let endDate = parseDate('01-06-2018');
let selectedMembers = this.members.filter(m => {
const current = parseDate(m.date);
return current > startDate && current < endDate;
});