-
-
Save ThemeCatcher/1bda93c11d7c426ed20eff555ce0b690 to your computer and use it in GitHub Desktop.
Calculate number of days between two date fields
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
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); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code can be added to Forms → Settings → Custom CSS & JS → Custom JavaScript.
1_3
with the unique ID of the Start Date field1_4
with the unique ID of the End Date field1_5
with the unique ID of the field to store the number of days (it can be a Hidden field or Text field)