1,105,295 Community Members

Add certain number of days to the already selected date

Member Avatar
inheaven
Newbie Poster
9 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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..??

Member Avatar
stbuchok
Posting Shark
957 posts since May 2011
Reputation Points: 123 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 2 [?]
 
0
 
Member Avatar
inheaven
Newbie Poster
9 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

getFullYear()

Look at this please: http://stackoverflow.com/questions/563406/add-days-to-datetime-using-javascript

Also use getFullYear(), not getYear()

http://www.w3schools.com/jsref/jsref_obj_date.asp

Look at this please: http://stackoverflow.com/questions/563406/add-days-to-datetime-using-javascript

Also use getFullYear(), not getYear()

http://www.w3schools.com/jsref/jsref_obj_date.asp

Thanks stbuchok

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: