Looking to check if string contains date using JS
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);
}