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

505
Views
¿Cómo obtener los registros del día anterior en MongodDB?

Estoy usando MongoDB que tiene un campo de tiempo en milisegundos. Estoy tratando de obtener todos los registros del día anterior. Hasta ahora he buscado soluciones que muestren cómo obtener los registros de las 24 horas anteriores. Pero necesito registros del día anterior solamente. Por favor ayuda a escribir esta consulta. Entonces algo como esto

 createdTime: { $lt: new Date().getTime(), $gt: new Date().getTime() - 1000 * 60 * 60 * 24, },

Pero como expliqué, esto no es lo que quiero.

over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

Puede obtener las marcas de tiempo de UNIX para el inicio y el final del día anterior de esta manera.
NOTA: comienza y termina en hora UTC

 var start = new Date().setUTCHours(0,0,0,0)-1000 * 60 * 60 * 24; var end = new Date().setUTCHours(23,59,59,999)-1000 * 60 * 60 * 24; console.log(start) console.log(end) console.log(new Date(start).toUTCString()) console.log(new Date(end).toUTCString())

over 4 years ago · Santiago Trujillo Report

0

var date = new Date(); date ; //# => today date var yesterdayDate = date.setDate(date.getDate() - 1); createdTime: { $lt: date, $gt: yesterdayDate, }
over 4 years ago · Santiago Trujillo Report

0

Asumiríamos que "el día anterior" es un período de 24 horas que comienza al comienzo del día anterior a hoy. La forma más fácil de hacer esto es usar el tiempo basado en UTC y agregar un desplazamiento de zona horaria (si es necesario).

 const timeZoneOffset = -5; createdTime: { $gt: new Date().setUTCHours(0,0,0)-1000 * 60 * 60 * (24 + timeZoneOffset), $lt: new Date().setUTCHours(23,59,59,999)-1000 * 60 * 60 * (24 + timeZoneOffset) },
over 4 years ago · Santiago Trujillo Report

0

Siempre que tenga que trabajar con aritmética de fecha/hora, le recomiendo la biblioteca moment.js :

 createdTime: { $lt: moment().endOf('day').subtract(1, 'day').toDate(), $gte: moment().startOf('day').subtract(1, 'day').toDate() },

Las bibliotecas alternativas son luxon y day.js

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!