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

210
Vistas
How to remove '-' from a date if it is the last character?

I have a function that receives 3 inputs from the user: year, month and day.

The user can only set the month if the year has been set. The day can also only be set if the month is set.

In the end, the object looks like this:

updatedFields.push({
    id: book.id,
    readIn: `${year}-${month}-${day}`,
});

The problem is that the user is not obliged to inform anything. So if it just informs the year and month or just the year, I'll have something like:

"2020--" // if he only enters the year
"2021-10-" // if he enters the year and month
"2022-03-13" // if it enters year, month and day

The question is: how do I remove the excess - dashes?

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

0

Construct an array from the possible values, filter out those that don't exist, then join it by the separator.

readIn: [year, month, day].filter(Boolean).join('-')
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use a regular expression to remove trailing characters.

updatedFields.push({
    id: book.id,
    readIn: `${year}-${month}-${day}`.replace(/-+$/, '')
});
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