old-website/scripts/mdx-scripts/parseTime.js

20 lines
666 B
JavaScript

const parseTime = (dateStr, timeStr) => {
if (/-/.exec(timeStr)) {
console.error(`${timeStr} has date a range`);
return;
}
timeStr = timeStr.toUpperCase() // converting cases like 7PM to 7PM
if (/[0-9](a|A|p|P)/g.exec(timeStr)) { // converting cases like 7PM to 7 PM
timeStr = timeStr.replace(/[0-9](?=(a|A|p|P))/g, "$& ");
}
if (/(?<!:[0-9])[0-9] (a|A|p|P)/g.exec(timeStr)) { // converting cases like 7 PM to 7:00 PM
timeStr = timeStr.replace(/[0-9](?= )/, "$&:00");
}
let date = new Date(`${dateStr} ${timeStr}`); // convert from CST to EST
date.setHours(date.getHours() + 1);
console.log(date.toString());
return date;
}