0

I use bootstrap datepicker and have following issue:

<input type="text" class="form-control" ng-click="viewModel.toggleDatepicker()" ng-model="viewModel.selectedDate" min-date="viewModel.datepicker.minDate" max-date="viewModel.datepicker.maxDate" datepicker-options="viewModel.datepicker.options"show-button-bar="false" />

and in Controller:

var today = this.selectedDate;
var nextWeek = this.selectedDate;
nextWeek.setDate(nextWeek.getDate() + this.DaysSlots);

after those lines gets executed both today and nextWeek points to the same date. Why is that?

Thanks for response

6
  • What does this.DaysSlots get for you?
    – Thalaivar
    Commented Jan 7, 2016 at 11:37
  • this.DaysSlots is const value of type number
    – pmalecki
    Commented Jan 7, 2016 at 11:52
  • this.selectedDate, i hope you get it as a Date Object... and you don't need to wrap it again with new Date(obj) right?
    – Thalaivar
    Commented Jan 7, 2016 at 12:07
  • 1
    can you console out (today and nextWeek) before setting nextWeek.setDate() to ensure the values are different.... and my guess is nextWeek and today are pointing to same reference which might be causing you this issue.
    – Thalaivar
    Commented Jan 7, 2016 at 12:22
  • 1
    right, reference was the issue. I didnt know that new variable assigned to original object still points to that object. Thanks Thalaivar
    – pmalecki
    Commented Jan 7, 2016 at 12:41

1 Answer 1

1

To make your overall experince with angular dates better, you should look into moment .js

http://momentjs.com/

It allows you to create, format, add and subtract dates easily and with minimal issues. Using the default date.setDate function can cause lots of issues

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.