最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin12PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far