Angular 2:如何过滤两个日期之间的记录?
Posted
技术标签:
【中文标题】Angular 2:如何过滤两个日期之间的记录?【英文标题】:Angular 2 : How to filter records between two dates? 【发布时间】:2018-09-30 21:12:51 【问题描述】:
"id":1,
"name":"Jack",
"date":"01-06-2017"
,
"id":2,
"name":"Allen",
"date":"07-08-2017"
,
"id":3,
"name":"Annie",
"date":"22-11-2017"
,
此 JSON 存储在“成员”数组中,我想根据开始和结束日期过滤成员,例如:
let startDate;
let endDate;
let selectedMembers = this.members.filter(m => m.date > startDate && m.date < endDate);
类似的东西..
【问题讨论】:
你了解过ts
或js
的基础知识吗?
【参考方案1】:
这是我的问题的解决方案:
members:any[] = [
"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);
【讨论】:
【参考方案2】:你需要首先将日期从字符串转换为日期类型,以便正确比较它们,然后执行以下操作:
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);
【讨论】:
【参考方案3】:要对存储为字符串的日期进行排序,您应该使用基于 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"
如果没有,您必须将字符串解析为日期对象并在比较中使用它们。像这样的:
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);
【讨论】:
很高兴听到这个消息。不要忘记给答案投票,这样其他人就可以知道这会对他们有所帮助【参考方案4】:您需要反转日期格式,然后将其转换为 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
【讨论】:
【参考方案5】:因为你使用的是法国日期格式,你需要在字符串和日期对象之间自己制作转换器。
然后你必须处理 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;
);
【讨论】:
以上是关于Angular 2:如何过滤两个日期之间的记录?的主要内容,如果未能解决你的问题,请参考以下文章