var initialTimeZone = "local";
var timeZoneSelectorEl = document.getElementById("kt_docs_fullcalendar_timezone_selector");
var calendarEl = document.getElementById("kt_docs_fullcalendar_timezone");
var todayDate = moment().startOf("day");
var YM = todayDate.format("YYYY-MM");
var YESTERDAY = todayDate.clone().subtract(1, "day").format("YYYY-MM-DD");
var TODAY = todayDate.format("YYYY-MM-DD");
var TOMORROW = todayDate.clone().add(1, "day").format("YYYY-MM-DD");
var eventsArray = [
...
];
// Initialize the external events -- for more info please visit the official site: https://fullcalendar.io/demos
var calendar = new FullCalendar.Calendar(calendarEl, {
timeZone: initialTimeZone,
headerToolbar: {
left: "prev,next today",
center: "title",
right: "dayGridMonth,timeGridWeek,timeGridDay,listWeek"
},
initialDate: TODAY,
navLinks: true, // can click day/week names to navigate views
editable: true,
selectable: true,
dayMaxEvents: true, // allow "more" link when too many events
eventTimeFormat: { hour: "numeric", minute: "2-digit", timeZoneName: "short" },
events: eventsArray,
});
calendar.render();
// when the timezone selector changes, dynamically change the calendar option
// --- more info on Select2 on Change event: https://select2.org/programmatic-control/events
$(timeZoneSelectorEl).on("change", function () {
calendar.setOption("timeZone", "UTC");
// Remove all events
const removeEvents = calendar.getEvents();
removeEvents.forEach(event => {
event.remove();
});
// Add events with new timezone offset
const newEvents = eventsArray;
newEvents.forEach(event => {
var start;
var end;
if(this.value < 0){
start = moment(event.start).subtract(this.value.replace(/\D/g,""), "seconds").format(getFormat(event.start));
end = event.end ? moment(event.end).subtract(this.value.replace(/\D/g,""), "seconds").format(getFormat(event.end)) : start;
} else {
start = moment(event.start).add(this.value, "seconds").format(getFormat(event.start));
end = event.end ? moment(event.end).add(this.value, "seconds").format(getFormat(event.end)) : start;
}
calendar.addEvent({
title: event.title,
start: start,
end: end
});
});
calendar.render();
});
// Dynamic date format generator
const getFormat = (d) => {
if(d.includes("T")){
return "YYYY-MM-DDTHH:mm:ss";
} else {
return "YYYY-MM-DD";
}
}