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

356
Vistas
date-fns "RangeError: Invalid time value"

I am trying to convert 18:00:00.000 to 24H (18:00) and 12H (6PM) formats using date-fns. However, the input value of 18:00:00.000 gives a "RangeError: invalid time value".

My code

dateFns.format(new Date(18:00:00.000), 'H:mm')

I have no problem if I pass the time in the dateTime format such as 2022-01-04T11:00:00.000Z but I just need the time (hours and minutes).

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

0

Your code is failing because new Date() requires a valid date time string to create a new date object. To fix this, simply formulate a current date string and attach the timestamp you require and pass it to the new Date() method. Then you can pass the date object to the format method to get the desired output.

Refer to this chart to format your dates as per your requirement.

const now = dateFns.format(new Date(), 'YYYY-MM-DD');
const custTime = '18:00:00.000';
const custDt = new Date(`${now} ${custTime}`);
console.log(dateFns.format(custDt, 'h:mm A'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/2.0.0-alpha0/date_fns.min.js" integrity="sha512-0kon+2zxkK5yhflwFqaTaIhLVDKGVH0YH/jm8P8Bab/4EOgC/n7gWyy7WE4EXrfPOVDeNdaebiAng0nsfeFd9A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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