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

276
Vistas
Luxon fromFormat pattern ignore time

When I try to create Date object from string format in Luxon I get invalid date.

Hours and minutes are not passed. This gives me an Invalid date

const dateString = '28.09.2022';
DateTime.fromFormat(dateString, 'dd.LL.yyyy HH:mm').toJSDate();
// Invalid date

Using moment library it seems that if hours and minutes are not passed it defaults to 00:00:00

const dateString = '28.09.2022';
moment(date, 'DD.MM.YYYY HH:mm').toDate();
// Wed Sep 28 2022 00:00:00 GMT+1000 (Australian Eastern Standard Time)

Is there some option to format both formats with hours and without in Luxon using one method as I was using moment in many places and it accepted 'DD.MM.YYYY HH:mm' format no matter if you pass HH:mm in string?

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

No, I fear that there is no native way to get moment(String, String) behaviour in Luxon. Moreover, it seems that there is no counterpart of moment(String, String[]) in Luxon.

Please note that, while "Moment's parser is very forgiving", Luxon Parsing section of the docs states that "Luxon is quite strict about the format matching the string exactly."

You can build your own custom method and implement the business logic you need, here a possible example:

const DateTime = luxon.DateTime;

const customParse = (dateString) => {
  let dt = DateTime.fromFormat(dateString, 'dd.LL.yyyy');
  if (dt.isValid) {
    return dt;
  }
  return DateTime.fromFormat(dateString, 'dd.LL.yyyy HH:mm');
}

let dateString = '28.09.2022';
console.log(customParse(dateString).toJSDate());
dateString = '28.09.2022 11:46';
console.log(customParse(dateString).toJSDate());
<script src="https://cdn.jsdelivr.net/npm/luxon@3.0.4/build/global/luxon.min.js"></script>

almost 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