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

133
Views
Looking to check if string contains date using JS

I am looking to check if a string contains a date in format MM/DD/YYYY, and then if it does convert it to YYYYMMDD000000

var date = "test->1/22/2022";

if date contains MM/DD/YYYY then set date from MM/DD/YYYY to YYYYMMDD000000

How would you do this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Just to be simple, you could use split.

var date = "test->1/22/2022";
let arr = date.split('->')[1].split('/')
console.log(arr[2]+arr[0]+arr[1]+"0".repeat(6))

about 4 years ago · Juan Pablo Isaza Report

0

This looks like a job for Regular Expressions!

date.matchAll(/(\d{1,2}\/\d{1,2}\/\d{4})/g);

Then you can use the Date object to convert it however you like:

let date = "test->1/22/2022",
    matches = date.matchAll(/(\d{1,2}\/\d{1,2}\/\d{4})/g);

for (let val of matches) {
    let valDate = new Date(val[0]),
        month = valDate.getMonth() + 1,
        day = valDate.getDate(),
        dateStr = ''
            + valDate.getFullYear()
            + (month < 10 ? '0' + month : month)
            + (day < 10 ? '0' + day : day)
            + '000000';
        
    console.log(dateStr);
}

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!