views:

543

answers:

3

Two input boxes: when I set the first one, I want the second one to be be one day after the date selected in the first. I am using Keith Wood's jquery plugin: http://keith-wood.name/datepickRef.html.

Using altField: just keeps the same in the two fields, but I want to get 1 day after in the second input field.

A: 

Take a look at my answer here - JQuery datepicker- 2 inputs/textboxes and restricting range

One way to do it would be to use the beforeShow property to restrict value in the second datepicker. Alternatively, define an onSelect handler that updates the second input when a date is chosen in the first.

Working Demo Note: this is based on the jQuery DatePicker and not Keith Woods'.

EDIT:

As stated on the datepick plugin page -

This plugin forms the basis for the jQuery UI Datepicker. It is made available as a separate plugin since the UI team desired simplified functionality for their version.

thus the code based on the jQuery UI Datepicker will work with Keith's datepicker.

Code from demo

$(function () 
{

    $('#txtStartDate, #txtEndDate').datepicker(
    {
        showOn: "both",
        dateFormat: "dd M yy",
        onSelect: insertOtherDate,
        firstDay: 1, 
        changeFirstDay: false
    });   
});

function insertOtherDate(value, date, inst) {

     var firstDate = new Date(value);
     var datepickerInput = (date.id === 'txtStartDate') ? 'txtEndDate' : 'txtStartDate';
     var dateAdjust = (date.id === 'txtStartDate') ? 1 : -1;
     var secondDate = new Date(firstDate.getFullYear(), firstDate.getMonth(), firstDate.getDate() + dateAdjust);   

     $('#' + datepickerInput).datepicker('setDate', secondDate);
}
Russ Cam
the range between the startDate and endDate could not be always 1. Cause the startDate and endDate have minDate and maxDate. When the endDate selected, the startDate could not change to "endDate - 1" and the startDate could be keep the same as before. when the startDate selected, the endDate should change to "the startDate + 1", so how to do thatMany thanks
Jason
If I've understood you correctly, endDate should be 1 day more than startDate but should not exceed the maxDate defined for endDate. Likewise, startDate should be 1 day less than endDate but not earlier than the minDate defined for startDate. Is that correct? If not, you'll need to clarify what the range between startDate and endDate can be.
Russ Cam
What happens when an endDate is chosen before a startDate?
Russ Cam
thank, i get your idea
Jason
@Jason - in the InsertOtherDate function above, you would need to check (a) for endDate, if secondDate is greater than maxDate or (b) for startDate, if secondDate is less than minDate. If those cases are true, set the date to maxDate or minDate, respectively.
Russ Cam
Yes. i did, thanks a lot
Jason
if it has helped, please upvote. If it solves the problem, please accept. This means that others finding this page will know how to implement similar functionality (*and I get some rep too!*) :)
Russ Cam
how to vote? where is that button?
Jason
i did, thanks agian
Jason
+1  A: 

Define an onSelect handler that updates the second field with a date that is one later than the one chosen for the first field.

tvanfosson
A: 

Create an onSelect event from the datepicker. In the callback read the value, convert the date to UNIX TIMESTAMP(you can use $.datepick.TIMESTAMP), add 24*60*60000 and configure the second input accordingly.

kgiannakakis
would this input the date with the same custom formatting as used in the first datepicker?
Russ Cam