Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda