I've checkhours string like shown below and I want to split by first occurrence of '-' character, however, I'm not getting the correct result as shown in below code. Also, I want to make an array like 11:30 AM – 4:00 PM 1st part and 5:00 PM – 12:00 AM 2nd part?
console.log("checkHours", checkHours);
let [start, end] = checkHours.split(' – ');
current log:
checkHours 11:30 AM – 4:00 PM, 5:00 PM – 12:00 AM
start, end 11:30 AM 4:00 PM, 5:00 PM
That is, I want the result to be like-> first element in array should be 11:30 AM – 4:00 PM and second element in array 5:00 PM – 12:00 AM. Then I can split on first element of array 11:30 AM – 4:00 PM by '-' which gives result start = 11:30 AM and end = 4:00 PM.
Option #1
join back the other splitted elements:
checkHours = "11:30 AM – 4:00 PM, 5:00 PM – 12:00 AM"; console.log("checkHours", checkHours); let [start, ...end] = checkHours.split(' – '); end = end.join(" - "); console.log(start); console.log(end);
Option #2: Don't use split
checkHours = "11:30 AM – 4:00 PM, 5:00 PM – 12:00 AM"; console.log("checkHours", checkHours); let start, end; if (checkHours.indexOf(" – ") > -1) { [start, end] = [checkHours.slice(0, checkHours.indexOf(" – ")), checkHours.slice(checkHours.indexOf(" – ") + 3)]; } console.log(start); console.log(end);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.