Add certain number of days to the already selected date (the date that is in the datepicker textbox currently)

1.3k Views Asked by At

I have already looked on several questions on stackoveflow but no one seems to be answering my question.

In my application, users often need to go to +1 or -1 day from the current selected date. For this, I want to make this process more quick by adding two image buttons for +1 and -1 operation on the right side and the left side respectively.

I have tried this:

$('#date').datepicker('setDate', '-1');

But it always set the +1 date from the current date (that is today -1, not the -1 from the selected date).

How can I achieve this..??
Any help would be appreciable.

Tried Later:

function AddDays(arg) {
    var d = $('#date').datepicker('getDate');
    var d = new Date(d.getYear(), d.getMonth(), d.getDay() + arg);
    $('#date').datepicker('setDate', d);
}

And Calling this function as AddDays(1) and AddDays(-1) on onclick event of image but It produces very strage result.
For Example if I set the current date to 25-03-13 that is in dd-mm-y format then clicking on -1 button is setting the date to 28-02-0-87. Clicking it on again will result in no change. And clicking it third time will result 01-02-0-87.

I can't get the point. What kind of behavior is this..??

1

There are 1 best solutions below

0
shashwat On BEST ANSWER

Finally I have written my solution. Here is the code

function AddDays(arg) {
    var d = $('#date').datepicker('getDate');
    d.setDate(d.getDate() + arg);
    $('#date').datepicker('setDate', d);
}

Where date is the id of my jquery-ui datepicker.

I am still looking for any inline code (single line) that I can put directly in onclick event of my next and previous buttons.