Help to perform start date and end date filter in javascript
I am trying to perform a filter between the start and end date chosen by the user. I have some objects that look like:
let objetos = [
{nome: 'teste01', data: '03/09/2019'}
{nome: 'teste02', data: '03/10/2019'}
{nome: 'teste03', data: '03/11/2019'}
{nome: 'teste04', data: '03/12/2019'}
{nome: 'teste05', data: '03/01/2020'}
]
What I expect is to return by the objects containing the following e.g. the objects named teste01 and teste02 that are within the given date range:
let dataInicial: string = '01/09/2019';
let dataFinal: string = '31/10/2019';
let objetosFiltrados = objetos.filter(result => {
return result.data >= dataInicial && result.data <= dataFinal;
})
console.log(objetosFiltrados);
But when I do this he brings me all the records, what is the correct way to do this please?
0
Author: Diego Estacho, 2019-09-03
1 answers
Can use this code that works:
let objetos = [
{nome: 'teste01', data: '03/09/2019'},
{nome: 'teste02', data: '03/10/2019'},
{nome: 'teste03', data: '03/11/2019'},
{nome: 'teste04', data: '03/12/2019'},
{nome: 'teste05', data: '03/01/2020'}
]
function converteData(DataDDMMYY) {
const dataSplit = DataDDMMYY.split("/");
const novaData = new Date(parseInt(dataSplit[2], 10),
parseInt(dataSplit[1], 10) - 1,
parseInt(dataSplit[0], 10));
return novaData;
}
let dataInicial = converteData('01/09/2019');
let dataFinal = converteData('31/10/2019');
let objetosFiltrados = objetos.filter(result => {
return converteData(result.data) >= dataInicial && converteData(result.data) <= dataFinal;
})
console.log(objetosFiltrados);
1
Author: Leonardo Getulio, 2019-09-03 17:30:47