Reputation: 579
I am new to angularJS and now i would like to how to add the year from current date.
Let's say i can get the user DOB and i want to add the year+1.
I have tried by using,
$scope.userdob = $rootScope.user.dob;
$scope.userdob.add(1,'years');
but it's not working.
Can anyone help me to know about this logic with example ?
TIA..,
Upvotes: 1
Views: 12889
Reputation: 1
Use .setDate()
function to increment the days and instead of +1 day and add +365 days it will give you date after 1 year.
Upvotes: -1
Reputation: 761
You should use .setDate()
function to increment the days.
I created an angular example here http://codepen.io/heshamelghandour/pen/WwodMP
Upvotes: 2
Reputation: 145994
It looks like $rootScope.user.dob
is an instance of moment
from the moment.js
library. I think your main problem is angular's change detection cannot detect when you mutate this instance. Because the instance remains the same but the internal underlying date value does change. Thus I'd suggest:
$scope.userdob = $rootScope.user.dob.clone().add(1,'years').valueOf();
That way angular will get a regular JavaScript Date object instead of a moment instance and it's change detection will work correctly.
FYI to accomplish the same with a standard javascript Date instance you can do:
var userdob = new Date();
userdob.setYear(userdob.getFullYear() + 1)
To advance the year by 1.
Upvotes: 1