Custom Dynamic Scheduler with Elementor, Flatpickr & Moment.js

In this Article, we are addressing the problem of dynamic date and time selection in scheduling scenarios, particularly for setting up meetings or appointments. The importance of this...

Share this post

Table of Contents

In this Article, we are addressing the problem of dynamic date and time selection in scheduling scenarios, particularly for setting up meetings or appointments. The importance of this functionality lies in the need for a flexible and user-friendly interface that allows users to choose meeting times based on their preferences and availability.
Here’s a breakdown of the key features and their significance:
  1. Dynamic Time Selection: The code uses the Flatpickr library to create date and time picker instances. The time selection is dynamic, with options rounded to the nearest 15 minutes. This ensures precision in scheduling and avoids unnecessary granularity in time selection.

  2. Default and Minimum Date: The options include a default date set to the current date and a minimum date of ‘today,’ preventing users from selecting past dates.

  3. Locale Settings: The locale settings ensure consistency in date representation, such as starting the week on Monday. This enhances user experience and aligns with common calendar conventions.

  4. Schedule Options: The code provides predefined schedule options like ‘now,’ ’15 minutes,’ ‘1 hour,’ and specific days and times. This feature streamlines the scheduling process by offering commonly used timeframes.

  5. Next Occurrence Calculation: The function calculateNextDay calculates the next occurrence of a specified day, considering the current date and time. This is especially useful for setting up recurring meetings on specific weekdays.

  6. Event Handling: The code includes event handling for changes in schedule selection and meeting length. This ensures that the end date and time are automatically adjusted based on user inputs.

  7. URL Parameter Handling: If URL parameters are present, the code triggers a change event, allowing for pre-filled values or actions based on external inputs. This can be useful for integrating the scheduling functionality with other parts of a web application.

1. First of all add these files to your page.

    
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css"> 
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.29.4/moment.min.js"></script>
    
   

2. Rounding off the Current Time

We’ll start by rounding off the current time to the nearest 15 minutes. Think of it like putting your meeting on a clock that loves multiples of 15.
    
     // Round off the time to the nearest 15 minutes
var date = moment();
var roundedTime = moment(date).add(15 - (date.minutes() % 15), 'minutes').startOf('minute');
    
   

3. Setting Comman Options for Flatpickr Calendar

Now, we’ll set up your calendar to look and feel just the way you want it. We’ll use some options to customize the default dates, minimum dates, and how the time appears.
    
     // Options to customize your calendar
var commonOptions = {
 defaultDate: roundedTime.toDate(),
 minDate: 'today',
 position: 'auto right',
 disableMobile: true,
 enableTime: true,
 time_24hr: true,
 minuteIncrement: 15,
 dateFormat: 'Y-m-d H:i:S',
 locale: {
  firstDayOfWeek: 1 // start week on Monday
 }
};
    
   

4. Initializing the Start and End Date Calendars

Time to create your special meeting calendar with Elementor! We’ll use easy tools to pick the start and end times that suit you.
    
     // Create your meeting calendar with Elementor
var start_date = flatpickr('#form-field-starting_date_time', commonOptions);
var end_date = flatpickr('#form-field-ending_date_time', commonOptions);
    
   

5. Calculating the "Selected Day" Occurence

Ever wished your calendar could suggest the best days for your meetings? Our code can do that! It calculates the best days for your meetings based on your preferences.
    
     // Function for Calculating the next occurrence of the specified day.
function calculateNextDay(dayName) {
 var today = moment(); // Get the current date and time
 var nextDay = today.clone().isoWeekday(dayName).startOf('day');

 // If the next occurrence is today or later in the week, set the time and return
 if (nextDay.isSameOrAfter(today)) {
  return nextDay.format('YYYY-MM-DD') + ' ' + roundedTime.format('HH:mm:ss');
 }

 // Move to the next week and set the time
 nextDay.add(1, 'week').isoWeekday(dayName); // move to the next occurrence in the next week
 nextDay.startOf('day').add(roundedTime.hours(), 'hours').add(roundedTime.minutes(), 'minutes');

 return nextDay.format('YYYY-MM-DD') + ' ' + roundedTime.format('HH:mm:ss');
}
    
   

6. Defining and Calculating Meeting Time options using Moment.js

We’ve made it super simple to choose meeting times. Pick from a list of options like ‘now’, ’15 minutes from now’, or even ‘next Monday at 10 am’. Just click and go!
    
     // Simple options for choosing meeting times
var scheduleOptions = {
 'now': roundedTime.toDate(),
 '15': roundedTime.toDate(),
 '1h': moment(roundedTime).add(1, 'hours').toDate(),
 '10amt': moment().add(1, 'days').hours(10).startOf('hour').toDate(),
 'mon': calculateNextDay('Monday'),
 'tue': calculateNextDay('Tuesday'),
 'wed': calculateNextDay('Wednesday'),
 'thu': calculateNextDay('Thursday'),
 'fri': calculateNextDay('Friday'),
 'sat': calculateNextDay('Saturday'),
 'sun': calculateNextDay('Sunday')
};
    
   

7. Your Calendar, Your Rules

With the below code, your calendar responds to your choices. Change the meeting length or pick a different day, and your calendar updates instantly!
    
     // Your calendar responds to your choices
jQuery('#form-field-schedule').on('change', function () {
 var selectedValue = jQuery(this).val();
 var newDate = scheduleOptions[selectedValue];
 start_date.setDate(newDate);
 calculateAndSetEndDate();
});

// Changing the End Date and Time depending on the Meeting Length.
jQuery('#form-field-meeting_length').on('change', function () {
 calculateAndSetEndDate();
});
    
   

8. Update the Meeting Options with URL parameters / query strings.

Want to set up your calendar using a web link? Easy! Our code even handles that, making it simple to share your meeting plans.
    
     // Triggers change event manually if URL parameters exist.
if (document.location.search.length) {
  jQuery('#form-field-schedule, #form-field-meeting_length').trigger('change');
}
    
   
Click here to see URL parameters example.
It will set Meeting length to 60 minutes and Schedule to Coming Monday, and Both Calenders will be updated with Dates and Time automatically.

Here is a Working Example:

Sumit Sharma

Sumit Sharma

Hi, My name is Sumit Sharma, I am a Sr. WordPress Front-end Web Developer with 10+ Years experience. I work as a Freelancer and I have an expertise in WordPress, Front-end Development, UI/UX Design, Elementor PRO, PSD/Figma/Adobe XD to HTML/WordPress Conversions and WooCommerce Websites.

Contact Me
RELATED ARTICLES

Leave a Reply

Your email address will not be published. Required fields are marked *