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

215
Visualizações
How to take the value from the last month using a timestamp?

It's the first time I come here so I hope my question won't be too bad. I'm trying to pick the data from last day, last week and last month that are stored in a mongo collection.
So actually I did:

  if(period.includes("daily")){
    //search for 24h before now
    DockerStatsToKeep.find({
      read: { "$gte": moment(new Date()).subtract(86400, 'second').unix()}
    });
  }else if (period.includes("weekly")) {
    //search for 168h before now (7days)
    DockerStatsToKeep.find({
      read: { "$gte": moment(new Date()).subtract(604800, 'second').unix()}
    });
  }else if (period.includes("monthly")) {
    //search for last month 
    DockerStatsToKeep.find({
      read: { "$gte": moment(new Date()).subtract(xx, 'second').unix()}
    });
  }else{
    console.error("this is not a valid period format, please use 'daily, weekly or monthly'");
  }

But I'm stuck for the last month and I can't find a way to do it. Because month are sometimes 30days or 31days or 29days

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In a momentjs you can subtract month as well:

moment(new Date()).subtract(1, 'month').unix()

It will also handle the transition between the years. E.g. subtract 1 month from date in January 2017 will return date in December 2016

See documentation on subtract

over 4 years ago · Santiago Trujillo 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