Using the Angular date $filter
actually i can get the current day, year and month
.
But, how can i get the next and previous day, year, month
?
This is the code i wrote but i dont know where to start with
$scope.month = $filter('date')(date, 'MMMM');//December-November like
$scope.day = $filter('date')(date, 'dd'); //01-31 like
$scope.year = $filter('date')(date,'yyyy');//2014 like
$scope.nextYear = Number($scope.year) + 1;
$scope.prevYear = Number($scope.year) - 1;
$scope.nextDay = ?
etc...
Do you have any idea?
You could do it like this, angular date $filter doesn't offer you any easier way of doing it, it just formats a date in a custom desired format.
Day:
var myDate = new Date();
var previousDay = new Date(myDate);
previousDay.setDate(myDate.getDate()-1);
var nextDay = new Date(myDate);
nextDay.setDate(myDate.getDate()+1);
Month:
var previousMonth = new Date(myDate);
previousMonth.setMonth(myDate.getMonth()-1);
var nextMonth = new Date(myDate);
nextMonth.setMonth(myDate.getMonth()+1);
Year:
var previousYear = new Date(myDate);
previousYear.setYear(myDate.getFullYear()-1);
var nextYear = new Date(myDate);
nextYear.setYear(myDate.getFullYear()+1);
$scope.month = $filter('date')(myDate, 'MMMM');//December-November like
$scope.day = $filter('date')(myDate, 'dd'); //01-31 like
$scope.year = $filter('date')(myDate,'yyyy');//2014 like
$scope.nextDay = $filter('date')(nexyDay, 'dd');
$scope.prevDay = $filter('date')(previousDay, 'dd');
$scope.nextMonth = $filter('date')(nextMonth, 'MMMM')
$scope.prevMonth = $filter('date')(previousMonth, 'MMMM')
$scope.nextYear = $filter('date')(nextYear,'yyyy');
$scope.prevYear = $filter('date')(previousYear,'yyyy');
If you are going to do this a lot, I suggest you create a service to implement this logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With