Skip to content

Instantly share code, notes, and snippets.

@ThemeCatcher
Created May 22, 2023 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThemeCatcher/1bda93c11d7c426ed20eff555ce0b690 to your computer and use it in GitHub Desktop.
Save ThemeCatcher/1bda93c11d7c426ed20eff555ce0b690 to your computer and use it in GitHub Desktop.
Calculate number of days between two date fields
jQuery(function ($) {
var $startDate = $('.quform-field-1_3'),
$endDate = $('.quform-field-1_4'),
$hiddenField = $('.quform-field-1_5'),
datePicker = $startDate.data('kendoDatePicker'),
options = $startDate.data('options') || {},
culture = options.locale || 'en-US',
format;
if (window.kendo && datePicker) {
format = options.format ? options.format : kendo.getCulture(options.locale).calendars.standard.patterns.d;
$startDate.add($endDate).on('keyup blur', onDateChange);
datePicker.bind('change', onDateChange);
}
function onDateChange() {
var start = $startDate.val(),
end = $endDate.val(),
days = '';
if (Quform.isNonEmptyString(start) && Quform.isNonEmptyString(end)) {
var startDate = kendo.parseDate(start, format, culture),
endDate = kendo.parseDate(end, format, culture);
if (startDate !== null && endDate !== null) {
days = daysBetweenTwoDates(startDate, endDate);
}
}
$hiddenField.val(days).triggerHandler('change');
}
function daysBetweenTwoDates(startDate, endDate) {
var fromDateUTC = Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate());
var endDateUTC = Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate());
return Math.ceil((+endDateUTC - +fromDateUTC) / kendo.date.MS_PER_DAY);
}
});
@ThemeCatcher
Copy link
Author

ThemeCatcher commented May 22, 2023

The code can be added to Forms → Settings → Custom CSS & JS → Custom JavaScript.

  • On line 2 replace 1_3 with the unique ID of the Start Date field
  • On line 3 replace 1_4 with the unique ID of the End Date field
  • On line 4 replace 1_5 with the unique ID of the field to store the number of days (it can be a Hidden field or Text field)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment