Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda