Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
Filtering elements of array on the basis of dates

I have a array as follows:

data = [
    {
      "startDate": "2021-08-18T00:00:04.498059"
      "endDate": "2021-08-19T00:00:04.4962889"
    },
    {
      "startDate": "2021-08-18T00:00:04.498059"
      "endDate": "2021-08-19T00:00:04.4962889"
    }
    ]

    newArray = [];
    this.data.foreach(element => {
       if((element.startDate - element.endDate) > 7) {
           this.newArray.push(element);
       }
    })

I want to traverse the above array and check if difference between startDate and endDate is greater than 7 days for any element, than push that element to a new array.

Because of the date format I don't know my approach is correct or not. How can I do this correctly?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Simple JavaScript logic.

  • Calculate time difference in millisecond between start date and end date.
  • Convert the time into days by dividing with (1000 * 60 * 60 * 24).

Logic for (1000 * 60 * 60 * 24) => 1000 ms = 1 second, 60 seconds = 1 minute, 60 minute = 1 hour, 24 hours = 1 day.

const data = [
    { "startDate": "2021-08-18T00:00:04.498059", "endDate": "2021-08-19T00:00:04.4962889" },
    { "startDate": "2021-08-18T00:00:04.498059", "endDate": "2021-08-26T00:00:04.4962889" },
    { "startDate": "2021-08-18T00:00:04.498059", "endDate": "2021-08-19T00:00:04.4962889" }
];
const newArray = [];
data.forEach(element => {
    const diffTime = new Date(element.endDate) - new Date(element.startDate);
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
    if (diffDays > 7) {
        newArray.push(element);
    }
});
console.log(newArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think this is the one you are looking for:

let data = [
{
  startDate: "2021-08-18T00:00:04.498059",
  endDate: "2021-08-19T00:00:04.4962889",
},
{
  startDate: "2021-08-11T00:00:04.498059",
  endDate: "2021-08-19T00:00:04.4962889",
}
]


let newArray = [];
data.forEach(element => {
let start = new Date(element.startDate)
let end = new Date(element.endDate)
if((end-start)/1000/60/60/24 > 7)
      newArray.push(element);
   })
   console.log(newArray)

about 4 years ago · Juan Pablo Isaza Relatório

0

const data = [
{
  "startDate": "2021-08-18T00:00:04.498059",
  "endDate": "2021-08-19T00:00:04.4962889"
},
{
  "startDate": "2021-08-18T00:00:04.498059",
  "endDate": "2021-08-19T00:00:04.4962889"
}
]
const newData = [];
data.forEach(element => {
  const startDate = new Date(element.startDate);
  const endDate = new Date(element.endDate);
  const difference = endDate.getTime() - startDate.getTime();
  if (difference > 604800000) {
    newData.push(element);
  }
}
);
console.log(newData);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda