The following example displays how you can synchronise two date pickers together. A common scenario for an online date picker is when booking flights or something where you need to input outbound and inbound dates. The start date must be before the end date and vice versa. We can enforce this rule with a very simple little bit of script. It also has a field below the date pickers which allows you to see the number of days selected.
$(function() { $('.date-pick').datePicker() var $startDate = $('#start-date'); $endDate = $('#end-date'); $startDate.bind( 'dpClosed', function(e, selectedDates) { var d = selectedDates[0]; if (d) { d = new Date(d); $endDate.dpSetStartDate(d.addDays(1).asString()); var endDate = Date.fromString($endDate.val()); updateDuration(d, endDate); } } ); $endDate.bind( 'dpClosed', function(e, selectedDates) { var d = selectedDates[0]; if (d) { d = new Date(d); $startDate.dpSetEndDate(d.addDays(-1).asString()); var startDate = Date.fromString($startDate.val()); updateDuration(startDate, d); } } ); function updateDuration(startDate, endDate) { if (startDate.getFullYear() > 1 && endDate.getFullYear() > 1) { var numDays = ((endDate.getTime() - startDate.getTime()) / 86400000) + 1; $('#duration').html(numDays + ' days selected'); } } });
/* located in demo.css and creates a little calendar icon * instead of a text link for "Choose date" */ a.dp-choose-date { float: left; width: 16px; height: 16px; padding: 0; margin: 5px 3px 0; display: block; text-indent: -2000px; overflow: hidden; background: url(calendar.png) no-repeat; } a.dp-choose-date.dp-disabled { background-position: 0 -20px; cursor: default; } /* makes the input field shorter once the date picker code * has run (to allow space for the calendar icon */ input.dp-applied { width: 140px; float: left; }