Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // These two datepickers will help each other (and the user) to select a time period.
- // The 'StartDate' will always be before 'EndDate', and 'EndDate' will always be after 'StartDate'.
- // You can remove the 'minDate' restriction on 'EndDate', by commenting out or removing the following line in
- // the 'onClose' function:
- // $('#EndDate').datepicker('option', 'minDate', newMinDate);
- $('#StartDate').datepicker({
- dateFormat: 'yy-mm-dd',
- changeMonth: true,
- changeYear: true,
- showButtonPanel: true,
- showMonthAfterYear: true,
- showWeek: true,
- showAnim: "drop",
- constrainInput: true,
- minDate: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()),
- maxDate: new Date((new Date().getFullYear() + 2), new Date().getMonth(), new Date().getDate()),
- defaultDate: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()),
- onClose: function (dateText, inst) {
- // When onClose is called after we have clicked a day (and not clicked 'Close' or outside the datepicker), the input-field is automatically
- // updated with a valid date-string. They will always pass.
- // This try is to catch and handle the situations, where you open the datepicker, and manually type in an invalid date in the field,
- // and then close the datepicker by clicking outside the datepicker, or click 'Close', in which case no validation takes place.
- try {
- // If datepicker can parse the date using our formatstring, the instance will automatically parse
- // and apply it for us (after the onClose is done). Otherwise this will throw an exception, and go to our catch.
- var typedDate = $.datepicker.parseDate($(this).datepicker('option', 'dateFormat'), $(this).val());
- if (typedDate == null)throw "No date selected";
- // We do a manual check to see if the date is within minDate and maxDate, if they are defined.
- // If all goes well, the default date is set to the new date, and datepicker will apply the date for us.
- var minDate = $(this).datepicker("option", "minDate");
- var maxDate = $(this).datepicker("option", "maxDate");
- if (minDate !== null && typedDate < minDate) throw "Date is lower than minDate!";
- if (maxDate !== null && typedDate > maxDate) throw "Date is higher than maxDate!";
- // We update the default date, because the date seems valid.
- // We do not need to manually update the input-field, as datepicker has already done this automatically.
- $(this).datepicker('option', 'defaultDate', typedDate);
- }
- catch (err) {
- // Standard behavior is that datepicker does nothing to fix the value of the input field, until you choose
- // a new valid date, by clicking on a day.
- // Instead, we set the current date, as well as the value of the input-field, to the last selected (and
- // accepted/validated) date from the datepicker, by getting its default date. This only works, because
- // we manually change the default date of the datepicker whenever a new date is selected, in both 'beforeShow'
- // and 'onClose'.
- var date = $(this).datepicker('option', 'defaultDate');
- $(this).val($.datepicker.formatDate($(this).datepicker('option', 'dateFormat'), date));
- $(this).datepicker('setDate', date);
- }
- /*
- * This last bit of 'onClose' is only for when you want to update the properties of another datepicker, to match the date
- * being set on this datepicker. This is for when you want to use two datepickers to define a timeperiod, and #EndDate should
- * never have a date set to before #StartDate's date.
- */
- var newMinDate = $(this).datepicker('getDate');
- // If the end-date datepicker hasn't been opened yet, then 'getDate' will return null in here, even if you've initialized it using 'setDate'
- // later on in document.ready.
- // Therefore, we need to initialize its date with its default value here, if its date is null. Otherwise we will see weird behavior.
- // NOTE: 'getDate' will NOT return null, if you call it from an outside function, after you've initialized it using 'setDate'.
- // It is only a problem inside internal datepicker functions. That's some weirdness for you right there!
- if ($('#EndDate').datepicker('getDate') == null) {
- // If the date is null, we set #EndDate to its defaultDate.
- $('#EndDate').datepicker('setDate', $('#EndDate').datepicker('option', 'defaultDate'));
- }
- // Then we set #end's minDate to be the date we just selected in this datepicker.
- // NOTE: If you remove just this line, then the #end datepicker won't be restricted, by having its minDate set.
- // The code after this line will still help the user, by changing the selected and default dates in #end, to one that is
- // compatible with 'newMinDate', if it is currently set to be before 'newMinDate'.
- // If you want a less restrictive, yet STILL error-protected design, feel free to remove this line.
- $('#EndDate').datepicker('option', 'minDate', newMinDate);
- // If #end's selected date is before the new minDate, we set its defaultDate and current date to the new minDate. Otherwise, we leave it be.
- if($('#EndDate').datepicker('option', 'defaultDate') < newMinDate) {
- $('#EndDate').datepicker('option', 'defaultDate', newMinDate);
- $('#EndDate').datepicker('setDate', newMinDate);
- }
- },
- beforeShow: function (input, inst) {
- // beforeShow is particularly irritating when initializing the input-field with a date-string.
- // The date-string will be parsed, and used to set the currently selected date in the datepicker.
- // BUT, if it is outside the scope of the minDate and maxDate, the text in the input-field is not
- // automatically updated, only the internal selected date, until you choose a new date (or, because
- // of our onClose function, whenever you click close or click outside the datepicker).
- // We want the input-field to always show the date that is currently chosen in our datepicker,
- // so we do some checks to see if it needs updating. This may not catch ALL cases, but these are
- // the primary ones: invalid date-format; date is too early; date is too late.
- try {
- // If datepicker can parse the date using our formatstring, the instance will automatically parse
- // and apply it for us. Otherwise this will throw an exception, and go to our catch.
- var typedDate = $.datepicker.parseDate($(this).datepicker('option', 'dateFormat'), $(this).val());
- // We do a manual check to see if the date is within minDate and maxDate, if they are defined.
- if (typedDate == null)throw "No date selected";
- // If all goes well, the default date is set to the new date, and datepicker will apply the date for us.
- var minDate = $(this).datepicker("option", "minDate");
- var maxDate = $(this).datepicker("option", "maxDate");
- if (minDate !== null && typedDate < minDate) throw "Date is lower than minDate!";
- if (maxDate !== null && typedDate > maxDate) throw "Date is higher than maxDate!";
- // We update the default date, because the date seems valid.
- // We also manually update the input-field, as datepicker does not automatically do this when opened.
- $(this).val($.datepicker.formatDate($(this).datepicker('option', 'dateFormat'), typedDate));
- $(this).datepicker('option', 'defaultDate', typedDate);
- }
- catch (err) {
- // Standard behavior is that datepicker does nothing to fix the value of the input field, until you choose
- // a new valid date, by clicking on a day.
- // We want the same behavior when opening the datepicker, so we set the current date, as well as the value
- // of the input-field, to the last selected (and accepted/validated) date from the datepicker, by getting
- // its default date. This only works, because we manually change the default date of the datepicker whenever
- // a new date is selected, in both 'beforeShow' and 'onClose', AND have a default date set in the datepicker options.
- var date = $(this).datepicker('option', 'defaultDate');
- $(this).val($.datepicker.formatDate($(this).datepicker('option', 'dateFormat'), date));
- $(this).datepicker('setDate', date);
- }
- /*
- * This last bit of beforeShow is only for when you want to help the user, by defaulting to a date that is compatible with the
- * date already selected in an end-datepicker. Meaning, if you select a date in the end-datepicker first, and then open this one,
- * you want this one to default to a date before the end-datepicker's date.
- */
- var endDate = $('#EndDate').datepicker('getDate');
- if (endDate !== null && endDate !== undefined) {
- var thisDate = $(this).datepicker("getDate");
- if (thisDate !== null) {
- if (thisDate > endDate) {
- $(this).datepicker('option', 'defaultDate', endDate);
- $(this).datepicker('setDate', endDate);
- }
- } else {
- $(this).datepicker('option', 'defaultDate', endDate);
- $(this).datepicker('setDate', endDate);
- }
- }
- }
- });
- $('#EndDate').datepicker({
- dateFormat: 'yy-mm-dd',
- changeMonth: true,
- changeYear: true,
- showButtonPanel: true,
- showMonthAfterYear: true,
- showWeek: true,
- showAnim: "drop",
- constrainInput: true,
- minDate: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()),
- maxDate: new Date((new Date().getFullYear() + 2), new Date().getMonth(), new Date().getDate()),
- defaultDate: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()),
- onClose: function (dateText, inst) {
- // When onClose is called after we have clicked a day (and not clicked 'Close' or outside the datepicker), the input-field is automatically
- // updated with a valid date-string. They will always pass.
- // This try is to catch and handle the situations, where you open the datepicker, and manually type in an invalid date in the field,
- // and then close the datepicker by clicking outside the datepicker, or click 'Close', in which case no validation takes place.
- try {
- // If datepicker can parse the date using our formatstring, the instance will automatically parse
- // and apply it for us (after the onClose is done). Otherwise this will throw an exception, and go to our catch.
- var typedDate = $.datepicker.parseDate($(this).datepicker('option', 'dateFormat'), $(this).val());
- if (typedDate == null)throw "No date selected";
- // We do a manual check to see if the date is within minDate and maxDate, if they are defined.
- // If all goes well, the default date is set to the new date, and datepicker will apply the date for us.
- var minDate = $(this).datepicker("option", "minDate");
- var maxDate = $(this).datepicker("option", "maxDate");
- if (minDate !== null && typedDate < minDate) throw "Date is lower than minDate!";
- if (maxDate !== null && typedDate > maxDate) throw "Date is higher than maxDate!";
- // We update the default date, because the date seems valid.
- // We do not need to manually update the input-field, as datepicker has already done this automatically.
- $(this).datepicker('option', 'defaultDate', typedDate);
- }
- catch (err) {
- // Standard behavior is that datepicker does nothing to fix the value of the input field, until you choose
- // a new valid date, by clicking on a day.
- // Instead, we set the current date, as well as the value of the input-field, to the last selected (and
- // accepted/validated) date from the datepicker, by getting its default date. This only works, because
- // we manually change the default date of the datepicker whenever a new date is selected, in both 'beforeShow'
- // and 'onClose'.
- var date = $(this).datepicker('option', 'defaultDate');
- $(this).val($.datepicker.formatDate($(this).datepicker('option', 'dateFormat'), date));
- $(this).datepicker('setDate', date);
- }
- /*
- * This last bit of 'onClose' is only for when you want to update the selected date of a start-datepicker, to match the date
- * being set on this end-datepicker. We merely help the user, by setting the default and current dates, to the same date as the
- * one we've just chosen, BUT ONLY IF the date of the start-datepicker is after (and therefore not compatible) with the
- * newly selected date for this datepicker.
- */
- var newMaxDate = $(this).datepicker('getDate');
- if ($('#StartDate').datepicker('getDate') == null) {
- // If the date is null, we set #start to its defaultDate.
- $('#StartDate').datepicker('setDate', $('#StartDate').datepicker('option', 'defaultDate'));
- }
- if($('#StartDate').datepicker('getDate') > newMaxDate) {
- $('#StartDate').datepicker('option', 'defaultDate', newMaxDate);
- $('#StartDate').datepicker('setDate', newMaxDate);
- }
- },
- beforeShow: function (input, inst) {
- // beforeShow is particularly irritating when initializing the input-field with a date-string.
- // The date-string will be parsed, and used to set the currently selected date in the datepicker.
- // BUT, if it is outside the scope of the minDate and maxDate, the text in the input-field is not
- // automatically updated, only the internal selected date, until you choose a new date (or, because
- // of our onClose function, whenever you click close or click outside the datepicker).
- // We want the input-field to always show the date that is currently chosen in our datepicker,
- // so we do some checks to see if it needs updating. This may not catch ALL cases, but these are
- // the primary ones: invalid date-format; date is too early; date is too late.
- try {
- // If datepicker can parse the date using our formatstring, the instance will automatically parse
- // and apply it for us. Otherwise this will throw an exception, and go to our catch.
- var typedDate = $.datepicker.parseDate($(this).datepicker('option', 'dateFormat'), $(this).val());
- if (typedDate == null)throw "No date selected";
- // We do a manual check to see if the date is within minDate and maxDate, if they are defined.
- // If all goes well, the default date is set to the new date, and datepicker will apply the date for us.
- var minDate = $(this).datepicker("option", "minDate");
- var maxDate = $(this).datepicker("option", "maxDate");
- if (minDate !== null && typedDate < minDate) throw "Date is lower than minDate!";
- if (maxDate !== null && typedDate > maxDate) throw "Date is higher than maxDate!";
- // We update the input-field, and the default date, because the date seems valid.
- // We also manually update the input-field, as datepicker does not automatically do this when opened.
- $(this).val($.datepicker.formatDate($(this).datepicker('option', 'dateFormat'), typedDate));
- $(this).datepicker('option', 'defaultDate', typedDate);
- }
- catch (err) {
- // Standard behavior is that datepicker does nothing to fix the value of the input field, until you choose
- // a new valid date, by clicking on a day.
- // We want the same behavior when opening the datepicker, so we set the current date, as well as the value
- // of the input-field, to the last selected (and accepted/validated) date from the datepicker, by getting
- // its default date. This only works, because we manually change the default date of the datepicker whenever
- // a new date is selected, in both 'beforeShow' and 'onClose', AND have a default date set in the datepicker options.
- var date = $(this).datepicker('option', 'defaultDate');
- $(this).val($.datepicker.formatDate($(this).datepicker('option', 'dateFormat'), date));
- $(this).datepicker('setDate', date);
- }
- /*
- * This last bit of beforeShow is only for when you want to help the user, by defaulting to a date that is compatible with the
- * date already selected in a start-datepicker.
- * Meaning, if the default date in the start-datepicker is after the date in this end-datepicker, and you onpe this one first,
- * you want the start-datepicker to default to a date before the end-datepicker's date. It's mostly a fail-safe.
- */
- if ($('#StartDate').datepicker('getDate') == null) {
- // If the date is null, we set #start to its defaultDate.
- $('#StartDate').datepicker('setDate', $('#StartDate').datepicker('option', 'defaultDate'));
- }
- var startDate = $('#StartDate').datepicker('getDate');
- if (startDate !== null && startDate !== undefined) {
- var thisDate = $(this).datepicker("getDate");
- if (thisDate !== null) {
- if (thisDate < startDate) {
- $(this).datepicker('option', 'defaultDate', startDate);
- $(this).datepicker('setDate', startDate);
- }
- } else {
- $(this).datepicker('option', 'defaultDate', startDate);
- $(this).datepicker('setDate', startDate);
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement