Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
JavaScript: is there a way to detect that a given date in such a format is within last X days?

I have a bunch of timestamp formatted like this

const dates = ['2021.6.01', '2021.6.11', '2021.9.02']

I wanted to write a util that can tell me if a date with such a format is within the last X days of another date.

For example, 2021.6.10 is within the last 7 days of 2021.6.12 while 2021.6.01 is not within the last 7 days of 2021.6.12.

I was think the API interface would be but please feel free to suggest a better naming

function isWithinTheLastDays(originalDate, date, days)

I found it really tricky to implement by hand and there are a lot of edge cases.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Parse the dates with the Date constructor, subtract the two dates and convert the millisecond difference to days (by dividing by 86400000), then check whether it is smaller or equal to days:

function isWithinTheLastDays(originalDate, date, days){
  return (new Date(date) - new Date(originalDate)) / 86400000 <= days;
}

console.log(isWithinTheLastDays('2021.6.10', '2021.6.12', 7)) //2 day diff
console.log(isWithinTheLastDays('2021.6.10', '2021.6.17', 7)) //7 day diff
console.log(isWithinTheLastDays('2021.6.10', '2021.6.18', 7)) //8 day diff

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try this:

Note: this will return true if the dates is X days after and before the date you've set

const dates = ['2021.08.26', '2021.6.11', '2021.9.02']

function isWithinDays({ originalDate, date, range }) {
  return parseInt(((new Date(date) - new Date(originalDate)) / (1000 * 60 * 60 * 24)), 10) <= range && parseInt(((new Date(date) - new Date(originalDate)) / (1000 * 60 * 60 * 24)), 10) >= 0 ? true : false
}

for (var i = 0; i < dates.length; i++) {
  console.log(isWithinDays({
    originalDate: dates[i],
    date: new Date('2021-08-27'),
    range: 10
  }))
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

turning it to Unix timestamp and subtracting it

function isWithinTheLastDays(originalDate, date, days){
    if(Math.abs(
    new Date(date).getTime()/1000
    - new Date(originalDate).getTime()/1000
    )/60/60/24 >= days){
        return true;
    }else{
        return false;
    };
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda