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?
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?
Share Improve this question asked Sep 8, 2014 at 7:48 Filippo orettiFilippo oretti 49.8k96 gold badges229 silver badges351 bronze badges 3-
1
you can use
http://www.datejs./
plugin :) orhttp://momentjs./docs/
:) – Kalhan.Toress Commented Sep 8, 2014 at 7:57 - +1 for using moment.js, you will save your time, simplify your code, and to the tricks rightly ! – benek Commented Sep 8, 2014 at 10:05
- @benek nah i am building a directive and it must work without helps ;) – Filippo oretti Commented Sep 8, 2014 at 10:53
1 Answer
Reset to default 17You 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.