使用角度日期$filter
实际上我可以获得当前的day, year and month
。
但是,我怎样才能获得下一个和之前的day, year, month
?
这是我写的代码,但我不知道从哪里开始
$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...
你有什么想法吗?
答案 0 :(得分:13)
你可以这样做,角度日期$ filter不提供任何更简单的方法,它只是以自定义的格式格式化日期。
天:
var myDate = new Date();
var previousDay = new Date(myDate);
previousDay.setDate(myDate.getDate()-1);
var nextDay = new Date(myDate);
nextDay.setDate(myDate.getDate()+1);
月:
var previousMonth = new Date(myDate);
previousMonth.setMonth(myDate.getMonth()-1);
var nextMonth = new Date(myDate);
nextMonth.setMonth(myDate.getMonth()+1);
年份:
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');
如果你打算这么做,我建议你创建一个服务来实现这个逻辑。