Last active
May 25, 2023 08:47
-
-
Save iamrealfarhanbd/d994f1cd9b89c551203a70f1b9885105 to your computer and use it in GitHub Desktop.
This code snippet demonstrates how to use the Flatpickr library with jQuery to create a range datepicker and dynamically display the selected date range as a message in a <p> element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
mode: "range", | |
onClose: function(selectedDates, dateStr, instance) { | |
if (selectedDates.length === 2) { | |
// Get the start and end dates | |
var startDate = selectedDates[0]; | |
var endDate = selectedDates[1]; | |
// Calculate the number of nights | |
var nightCount = Math.round( | |
(endDate - startDate) / (1000 * 60 * 60 * 24) | |
); | |
// Define a function to format the date as the day of the week | |
function formatDate(date) { | |
var options = { weekday: "long" }; | |
return date.toLocaleDateString("en-US", options); | |
} | |
// Format the start and end dates | |
var formattedStartDate = formatDate(startDate); | |
var formattedEndDate = formatDate(endDate); | |
// Create the selected date message | |
var selectedDateMessage = | |
"From: " + formattedStartDate + " to " + formattedEndDate + " (" + nightCount + " Nights)"; | |
// Check if the selected date element exists | |
var $selectedDateElement = $("#selected-date"); | |
if ($selectedDateElement.length) { | |
// Update the text content | |
$selectedDateElement.text(selectedDateMessage); | |
} else { | |
// Create a new <p> element with the selected date message | |
var $newSelectedDateElement = $("<p>", { | |
id: "selected-date", | |
text: selectedDateMessage, | |
}); | |
// Append the new element after the date range picker input ex: ff_3_datetime | |
$("#ff_YourFormId_datetime").after($newSelectedDateElement); | |
} | |
} | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code Updated now,output will be like: From: Monday to Friday (4 Nights)