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

133
Visualizações
How to get all of the past 30 days records in JSON array based their DD/MM/YYYY field?

Fixed old post. I am looking for clues and solutions that can help me get all the records in the JSON array from the past 30 days (based on the date_post field dd/mm/yyyy).

I have used getMonth() but I am getting an unexpected output due to the format mismatch. Is there a way to do this without having to swap dd and mm?

[
  {
    "id": "5537a23050b2c722f390ab60",
    "thumbImage": "http://lorempixel.com/175/115",
    "title": "reprehenderit nisi occaecat magna eiusmod officia qui do est culpa",
    "date_posted": "19/04/2020"
  }
]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Can't you simply write a loop, that iterates over your JSON array and checks whether the date is younger than 30 days?

Like so:

var currentDay=getDay(jsonArr.at(-1));
var currentMonth=getMonth(jsonArr.at(-1));
var lastEntries=[];
foreach(jsonArr => json){
    if(getDay(json) < currentDay && getMonth(json) == currentMonth || 
       getDay(json) > currentDay && getMonth(json) < currentMonth){ 
       lastEntries[]=json;
    }

And now you just have to implement the functions getDay() and getMonth():

function getDay(json){
   return json.date_posted.split('/')[0]; // and for the getMonth() function you take the [1] instead of [0]
}

This solution is not very clean and nice, but it should work.

about 4 years ago · Juan Pablo Isaza Relatório

0

You'll need to parse the date and then use Array.filter (or something like that). Here is one example:

const data = [
  {
    "id": "5537a23050b2c722f390ab60",
    "thumbImage": "http://lorempixel.com/175/115",
    "title": "reprehenderit nisi occaecat magna eiusmod officia qui do est culpa",
    "date_posted": "19/04/2020"
  },
  { "id": "2", "date_posted": "19/04/2021"},
  { "id": "2", "date_posted": "19/01/2019"},
  { "id": "2", "date_posted": "07/01/2022"},
];

const threshold = Date.now() - 30 * 24* 60 * 60 * 1000;
const filtered = data.filter(({date_posted}) => {
  const [day, month, year] = date_posted.split('/');
  return (new Date(`${year}-${month}-${day}`)) > threshold;
});
console.log(filtered);

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