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?
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))
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);
}