Skip to content

Commit

Permalink
update time selector formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyundai committed May 16, 2024
1 parent 312aa5c commit daaf86d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion blocks/event-info-component/event-info-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ function buildDatePicker(column) {
column.append(dateLabel, datePicker, calendarIcon);
}

function convertTo24HourFormat(timeStr) {
const timeFormat = /^(0?[1-9]|1[0-2]):([0-5][0-9]) (AM|PM)$/;

if (!timeStr.match(timeFormat)) {
throw new Error("Invalid time format. Expected format: 'h:mm AM/PM'");
}

const [time, period] = timeStr.split(' ');
const [, minutes] = time.split(':').map(Number);
let [hours] = time.split(':').map(Number);

if (period === 'PM' && hours !== 12) {
hours += 12;
} else if (period === 'AM' && hours === 12) {
hours = 0;
}

const formattedHours = hours.toString().padStart(2, '0');
const formattedMinutes = minutes.toString().padStart(2, '0');

return `${formattedHours}:${formattedMinutes}:00`;
}

function buildTimePicker(column) {
column.classList.add('time-pickers');
const header = column.querySelector(':scope > p');
Expand Down Expand Up @@ -78,7 +101,7 @@ function buildTimePicker(column) {
select.append(option);
timeSlots.forEach((t) => {
const text = t.textContent.trim();
const opt = createTag('option', { value: handlize(text) }, text);
const opt = createTag('option', { value: convertTo24HourFormat(text) }, text);
select.append(opt);
});
timePickerWrapper.append(select);
Expand Down

0 comments on commit daaf86d

Please sign in to comment.