Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Picking a start and end date within range based on start date

I'm building a date picker in jQuery ui. What I'm trying to do is set a range, so when they pick the first date the second date appears and gives a 30 day window. I tried this but it doesn't work (it lets the user pick 30 days from today, not 30 from the start date):

var pickDate;
$( "#datepickerEnd" ).hide();
$( "#datepickerStart" ).datepicker();
$( "#datepickerStart" ).change(function(){
    var pickDate = $( "#datepickerStart" ).val();
    $( "#datepickerEnd" ).datepicker({ minDate: pickDate, maxDate: +30 });
    $( "#datepickerEnd" ).show();
})

Another issue I ran into is when I change datepickerStart it'll only set the start range once but not every time I change it. I have to refresh the page to lock in a new start date.

like image 299
Matt Coady Avatar asked Jun 10 '13 03:06

Matt Coady


People also ask

How can I set start and end dates for a datepicker?

Setting a start and end datevar start_date = new Date(). increment('week', 1); var end_date = new Date(). increment('week', 5); This will allow the picking of dates between one week and five weeks in the future.

How do I restrict date range of a jQuery datepicker by giving two dates?

To implement this, write code in jQuery UI Datepicker "onSelect" event which gets called when date is selected. $(document). ready(function () { var daysToAdd = 4; $("#txtFromDate"). datepicker({ onSelect: function (selected) { var dtMax = new Date(selected); dtMax.

How to use jQuery date picker?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.

What is RTL datepicker?

Right-to-left (RTL) support reflects the ability of a widget to render its content in a right-to-left direction for right-to-left languages, such as Arabic, Hebrew, Chinese, or Japanese. For more information, refer to: RTL support by the DatePicker (demo)


1 Answers

Check this jsfiddle here. It's a working example of your problem.

HTML

<input type="text" id="dt1">
<input type="text" id="dt2">

JS

$(document).ready(function () {
    $("#dt1").datepicker({
        dateFormat: "dd-M-yy",
        minDate: 0,
        onSelect: function () {
            var dt2 = $('#dt2');
            var startDate = $(this).datepicker('getDate');
            //add 30 days to selected date
            startDate.setDate(startDate.getDate() + 30);
            var minDate = $(this).datepicker('getDate');
            var dt2Date = dt2.datepicker('getDate');
            //difference in days. 86400 seconds in day, 1000 ms in second
            var dateDiff = (dt2Date - minDate)/(86400 * 1000);

            //dt2 not set or dt1 date is greater than dt2 date
            if (dt2Date == null || dateDiff < 0) {
                    dt2.datepicker('setDate', minDate);
            }
            //dt1 date is 30 days under dt2 date
            else if (dateDiff > 30){
                    dt2.datepicker('setDate', startDate);
            }
            //sets dt2 maxDate to the last day of 30 days window
            dt2.datepicker('option', 'maxDate', startDate);
            //first day which can be selected in dt2 is selected date in dt1
            dt2.datepicker('option', 'minDate', minDate);
        }
    });
    $('#dt2').datepicker({
        dateFormat: "dd-M-yy",
        minDate: 0
    });
});

As soderslatt already mentioned use the onSelect option to set the dates. Other methods I used are:

  • getDate
  • setDate
  • minDate
  • maxDate

I think they're all very self explanatory and the documentation helps you to understand how they work. If you want to set the date of the second datepicker to dt1's date + 1 day do the same as in this line:

startDate.setDate(startDate.getDate() + 30);

But of course add 1 day and not 30.

like image 178
SirDerpington Avatar answered Oct 24 '22 21:10

SirDerpington