Datepicker JS
From GLMWiki
JQuery Datepicker Code Snippet (Object Literal Pattern)
var arrival_module = { init: function () { this.cacheDOM(); this.bindEvents(); }, cacheDOM: function() { this.$arrive = $("#arrive"); this.$currentDate = 0; }, bindEvents: function () { this.$arrive.datepicker({ minDate: 0, onSelect: function (date) { this.$currentDate = arrival_module.$arrive.datepicker('getDate'); this.$extraDay = arrival_module.$arrive.datepicker('getDate'); this.$extraDay.setDate(this.$extraDay.getDate() + 1); departure_module.$depart.datepicker("option", {minDate: new Date(this.$extraDay)}); } }); } }; var departure_module = { init: function () { this.cacheDOM(); this.bindEvents(); }, cacheDOM: function (){ this.$depart = $("#depart"); this.$departure = 0; }, bindEvents: function () { this.$depart.datepicker({ onSelect: function (date) { this.$departure = departure_module.$depart.datepicker('getDate'); } }); } }; departure_module.init(); arrival_module.init();
HTML5 Datepicker logic (Used for off canvas widgets) [needs to be refactored]
$(document).on('change', '#arriveOff', function () { currentDate = $("#arriveOff").val(); currentDate = new Date(currentDate); // this section is used to circumvent issues with selecting the last day of the month without a proper rollover to the following month. currentDate = new Date(currentDate.getUTCFullYear(), currentDate.getUTCMonth(), currentDate.getUTCDate()); currentDate.setDate(currentDate.getDate()); dd = currentDate.getDate() + 1; if (dd <= 9) { dd = '0' + dd; } d = currentDate.getDate(); if (d <= 9) { d = '0' + d; } m = currentDate.getMonth() + 1; if (m <= 9) { m = '0' + m; } y = currentDate.getFullYear(); requiredDate = y + '-' + m + '-' + d; minDays = y + '-' + m + '-' + dd; $("#departOff").attr("min", minDays); if (requiredDate <= $("#arriveOff").attr("min")) { minDays = $("#arriveOff").attr("min"); $("#arriveOff").val(minDays); } }); // restrict departure date /////////////////////////////// $(document).on('change', '#departOff', function () { currentDate2 = $("#departOff").val(); currentDate2 = new Date(currentDate2); currentDate2 = new Date(currentDate2.getUTCFullYear(), currentDate2.getUTCMonth(),currentDate2.getUTCDate()); currentDate2.setDate(currentDate2.getDate()); if($("#departOff").val() <= requiredDate){ $("#departOff").val(minDays); } });