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

106
Vistas
Updating to v2 date-fns: NaN

I'm updating the module date-fns from v1 to v2.

This helper method used to work:

const { format, parseISO, differenceInSeconds } = require("date-fns");

const newDateNow = () => {
    const date = new Date();
    return format(date, process.env.DATE_FORMAT);
};

// `process.env.DATE_FORMAT` is set to `yyyy-MM-dd HH:mm:ss`
console.log(ff) //prints: Thu Jul 21 2022 14:44:56 GMT+0200 (Central European Summer Time)
console.log(gg) //prints: 3600
console.log(newDateNow()) //prints: 2022-07-21 15:32:05

const isExpired = () => {
    const exp = differenceInSeconds(newDateNow(), ff);
    return parseInt(gg) > exp;
}

After the update to the module's v2 this generates the error: date-fns doesn't accept strings as date arguments. Please use 'parseISO' to parse strings. This pointing to the const exp line.

So I change that line to:

const exp = differenceInSeconds(newDateNow(), parseISO(ff));

This still generates the same error, so I change it to:

const exp = differenceInSeconds(parseISO(newDateNow()), parseISO(ff));

Now there is no error, but console.log(exp) prints NaN.

How should I implement this? Shouldn't I change the newDateNow method so that it's not necessary to use parseISO() on newDateNow()?

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

differenceInSeconds() is a function which takes two Date objects as arguments. In your code, newDateNow() is returning a string because you’re calling format() on the date, so you’re passing it a string rather than a date.

So based on just needing to compare the two dates you should skip that call to format() entirely.

Your code sample doesn’t include the assignment of ff, you will need to ensure that is a date and not a string as well.

If you haven't seen already, the documentation is handy: https://date-fns.org/v2.28.0/docs/Getting-Started

about 4 years ago · Santiago Gelvez 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