angularjs date格式在控制器中不能用于格式yyyy / MM / dd

时间:2017-08-03 04:02:57

标签: javascript angularjs date-format

格式化时, angularjs中的日期格式无效:yyyy/MM/dd

datepicker ng-model="Periode1" = 01 January 2016

的日期

我试过了:

var p1 = new Date($scope.Periode1);
$scope.period1Convert = $filter('date')(p1, "yyyy/MM/dd");

output : null

还有:

var d = new Date($scope.Periode1);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
$scope.period1Convert = (curr_year + "/" + curr_month + "/" + curr_date);

output : NaN/NaN/NaN

请帮帮我。谢谢:))

1 个答案:

答案 0 :(得分:0)

你过滤器工作正常。确保在执行过滤器之前将值赋给ng-model变量。

angular.module("app",[])
.controller("ctrl",function($scope, $filter){

$scope.Periode1 = "01 January 2016";
var p1 = new Date($scope.Periode1);
$scope.period1Convert = $filter('date')(p1, "yyyy/MM/dd");
console.log($scope.period1Convert)
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 
</div>