function deserializeTimeValues(timeStrings) {
const parsedTimes = [];
for (const timeString of timeStrings) {
try {
// Attempt to parse the time string using a regular expression.
const match = timeString.match(/^(\d{2}):(\d{2})$/);
if (match) {
const hours = parseInt(match[1], 10);
const minutes = parseInt(match[2], 10);
if (hours >= 0 && hours <= 23 && minutes >= 0 && minutes <= 59) {
parsedTimes.push({ hours, minutes });
} else {
throw new Error("Invalid time format: Hours must be between 0-23 and minutes between 0-59.");
}
} else {
// Handle cases where the time string doesn't match the expected format.
throw new Error("Invalid time format: Please use HH:MM format.");
}
} catch (error) {
// Catch any errors during parsing and store an error message.
parsedTimes.push(`Error: ${error.message}`);
}
}
return parsedTimes;
}
Add your comment