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

124
Visualizações
Compare Date Time returned from API using only JavaScript and React

I've got an API that returns me date time data like this: "2021-09-29T08:00:00" I want to filter those dates and return all the dates, before today. I tested a function like this:

  const currentDate = new Date();
  const results = licitacoes.map((item) => {
    const dateTime = new Date(item.licitacoesDate);
    if (currentDate < dateTime) {
      console.log("there are dateTime bigger than currentDate");
    } else {
      console.log("currentDate is bigger than DateTime");
    }
  });
That works fine inside my code. But what I really want is to display all the dates that are bigger than current date, when the user selects an input option. So I tried this code here:

 const currentDate = new Date();
 licitacoes.map((item) => {
       const dateTime = new Date(item.licitacoesDate);
      console.log(currentDate < dateTime)
    });
Which returns me false and give me a error, so I also tried this one:

const currentDate = new Date();
licitacoes
      .filter((bigN) => new Date(bigN.licitacoesDate) < currentDate)
      .map((newNumber) => console.log(newNumber.cityName));

Which does not work if I test bigN.licitacoes.date > currentDate And also returns me a giant error, but display the city names which are still available. PS: This code will be displayed inside a if statement of a select button.

The licitacoes would be a json file like that: licitacoes = [ { "id": "1" cityName: "London" "licitacoesDate": "2021-04-05T08:00:00" deliveryDate:"2021-05-12T08:00:00" sourceUrl: "www.google.com" licitacoesContent: "lorem ipsum" } ]

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

0

The problem, I think is: you don't have the data in your results? That is because you didn't return the value from map function, so all you need is return statement.

If you want to collect only time from licitacoes, you can do this:

licitacoes = [ { "id": "1", cityName: "London", "licitacoesDate": "2021-04-05T08:00:00", deliveryDate:"2021-05-12T08:00:00", sourceUrl: "www.google.com",licitacoesContent: "lorem ipsum" } ]


const currentDate = new Date();
const dates = licitacoes.map((item) => {
    const dateTime = new Date(item?.licitacoesDate);
        if (currentDate > dateTime) {
            return item.licitacoesDate
        }

    });
console.log(dates)

better way is filter first then return value as you did:

licitacoes = [ { "id": "1", cityName: "London", "licitacoesDate": "2021-04-05T08:00:00", deliveryDate:"2021-05-12T08:00:00", sourceUrl: "www.google.com",licitacoesContent: "lorem ipsum" } ]


const currentDate = new Date();
const dates = licitacoes
  .filter((bigN) => new Date(bigN.licitacoesDate) < currentDate)
  .map((newNumber) => newNumber.licitacoesDate);
console.log(dates)

If you want to return specific data you may do that:

licitacoes = [ { "id": "1", cityName: "London", "licitacoesDate": "2021-04-05T08:00:00", deliveryDate:"2021-05-12T08:00:00", sourceUrl: "www.google.com",licitacoesContent: "lorem ipsum" } ]


const currentDate = new Date();
const dates = licitacoes
  .filter((bigN) => new Date(bigN.licitacoesDate) < currentDate)
  .map((data) => {
      return {
         time: data.licitacoesDate,
         city: data.cityName,
         sourceUrl: data.sourceUrl
              }
});
console.log(dates)

Or you can just filter licitacoes without map, and it will return the array with all filtered items.

about 4 years ago · Juan Pablo Isaza Relatório

0

  • For bigger than current date: >
  • .map() return Array, use forOf to show data.
const currentDate = new Date();
const filtered = licitacoes.filter((item) => new Date(item.licitacoesDate) > currentDate);
for(item of filtered){console.log(item.cityName)}
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