最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs next and previous day,year,month - Stack Overflow

programmeradmin7浏览0评论

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 :) or http://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
Add a ment  | 

1 Answer 1

Reset to default 17

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.

发布评论

评论列表(0)

  1. 暂无评论