Skip to content

Commit

Permalink
update event date storage format
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyundai committed May 6, 2024
1 parent 93dd0fd commit 9a5a8d3
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions blocks/form-handler/controllers/event-info-component-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { getMappedInputsOutput } from './share-controller.js';

const { createTag } = await import(`${getLibs()}/utils/utils.js`);

function formatDate(date) {
let month = '' + (date.getMonth() + 1),
day = '' + date.getDate(),
year = date.getFullYear();

if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;

return [month, day, year].join('-');
}

function compareDates(date1, date2) {
return date1.getFullYear() === date2.getFullYear()
&& date1.getMonth() === date2.getMonth()
Expand Down Expand Up @@ -61,7 +74,7 @@ function updateDayView(component, parent, state) {
const dayElement = createTag('div', {
class: 'calendar-day',
tabindex: '0',
'data-date': date,
'data-date': formatDate(date),
}, day.toString(), { parent: calendarGrid });

if (date < todayDate) {
Expand Down Expand Up @@ -158,19 +171,15 @@ function updateInput(component, state) {
year: 'numeric',
month: 'long',
day: 'numeric',
timeZone: state.timeZone,
};

const dateInput = component.querySelector('#event-info-date-picker');

dateInput.dataset.startDate = state.selectedStartDate;
dateInput.dataset.endDate = state.selectedEndDate;

const localStartTime = state.selectedStartDate?.toLocaleString('en-GB', options) || '';
const localEndTime = state.selectedEndDate?.toLocaleString('en-GB', options) || '';
dateInput.dataset.startDate = formatDate(state.selectedStartDate);
dateInput.dataset.endDate = formatDate(state.selectedEndDate);

if (dateInput) {
dateInput.value = `${localStartTime} - ${localEndTime}`;
dateInput.value = `${dateInput.dataset.startDate} - ${dateInput.dataset.endDate}`;
}
}

Expand Down Expand Up @@ -225,7 +234,6 @@ function buildCalendar(component, parent) {
currentView: 'days',
selectedStartDate: input.dataset.startDate ? new Date(input.dataset.startDate) : null,
selectedEndDate: input.dataset.endDate ? new Date(input.dataset.endDate) : null,
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
currentYear: new Date().getFullYear(),
currentMonth: new Date().getMonth(),
headerTitle: createTag('span', { class: 'header-title' }, '', { parent }),
Expand Down

0 comments on commit 9a5a8d3

Please sign in to comment.