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

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

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 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!