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

330
Vistas
parse function in date-fns returns one day previous value

I am trying to parse date using date-fns library but getting one day previous as result. How can I avoid this and get the right result?

start = '2021-08-16'
const parseStart = parse(start, 'yyyy-MM-dd', new Date());

output:

2021-08-15T18:30:00.000Z
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Not to have this timezone overhead I'd suggest formatting your date string into ISO string and then using parseISO function of date-fns.

Like this:

import { parseISO } from 'date-fns';

const parseStringDate = (dateString: string): Date => {
  const ISODate = new Date(dateString).toISOString();
  return parseISO(ISODate);
};

parseStringDate("2021-08-19") //2021-08-19T00:00:00.000Z
over 4 years ago · Santiago Trujillo Denunciar

0

We had a similar problem while trying to use the date-fns format function on a hyphened string date. It was subtracting one day from the date while formating. The solution was in fact to change the hypes for slashes.

const dateString = '2010-08-03'
const date = new Date(dateString.replace(/-/g, '/'))
over 4 years ago · Santiago Trujillo Denunciar

0

This worked for me (v.2+):

// wrong:
const date = format(new Date('2021-10-01'),'dd/MM/yyyy')
console.log(date) // "30/09/2021"

// ok:
const date = format(new Date('2021/10/01'),'dd/MM/yyyy')
console.log(date) // "01/10/2021"
over 4 years ago · Santiago Trujillo 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