1

I am trying to add 3 days to the date I get from the jQuery Datepicker as a variable as I show in this example:

var dateSelected = fromDateInput.datepicker('getDate');
var count = 3;

var lol = dateSelected.setDate(dateSelected.getDate() + count);
console.log(lol);

If I use the variable I will get this in the console for example: 1449615600000.

If I do it like this:

var dateSelected = fromDateInput.datepicker('getDate');
var count = 3;

dateSelected.setDate(dateSelected.getDate() + count);
console.log(dateSelected);

I will get the correct date (The date I select in the datepicker + 3 days)

Why can't I use it in a variable?

3 Answers 3

1

Try this:

var dateSelected = fromDateInput.datepicker('getDate');
var count = 3;

var dateUpdated = dateSelected.getDate() + count;
dateSelected.setDate(dateUpdated);
console.log(dateUpdated);

The setDate method doesn't return anything. See the documentation: jQuery UI DatePicker setDate

Sign up to request clarification or add additional context in comments.

Comments

0

Try This:

var d = new Date("mm/dd/yyyy");
d.setDate(d.getDate()+3);

1 Comment

It would be good if you put up some more explanations with your answer.
0

The result of setDate is the number of milliseconds since midnight 1st January 1970. In your second example you are using the actual date object, which is why the second example gives you what you want and the first does not.

1 Comment

@krlzlx the getDate method on the jquery object returns a javascript date object. The subsequent call to setDate is called on the Date object not on the JQuery object, therefore it is incorrect to say setDate doesn't return anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.