Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!