Home > OS >  Compare Date Time returned from API using only JavaScript and React
Compare Date Time returned from API using only JavaScript and React

Time:10-01

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" } ]

CodePudding user response:

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.

CodePudding user response:

  • 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)}
  • Related