Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!