以2016年4月8日格式转换日期'在Angularjs?

时间:2016-04-08 06:38:17

标签: angularjs

如何将日期转换为2016年4月8日格式?

// in your controller:  
$scope.date = '20140313T00:00:00';

// in your view, date property, filtered with date filter and format 'MM dd yyyy' 

<div ng-bind="date | date:'MM dd yyyy'"></div>

2 个答案:

答案 0 :(得分:1)

角度控制器中的

        $scope..DayofDate = "";
        $scope.Monthofdate = "";
        $scope.DaySuffix = "";
        $scope.getDateFormat = function (date) {
            var suffixes = ["th", "st", "nd", "rd"];            
            var dtfilter = $filter('date')(date, 'dd');
            $scope.DayofDate = dtfilter;
            var mtfilter = $filter('date')(date, 'MMM yyyy');
            $scope.Monthofdate = mtfilter;
            var day = parseInt(dtfilter.slice(-2));
            var relevantDigits = (day < 30) ? day % 20 : day % 30;
            var suffix = (relevantDigits <= 3) ? suffixes[relevantDigits] : suffixes[0];
            $scope.DaySuffix = suffix;           
        }

并在您的html页面中

<div ng-init="getDateFormat(date)">
  <p>Date : {{DayofDate}} <sup>{{DaySuffix}}</sup>{{Monthofdate}}</p>
</div>

答案 1 :(得分:0)

试试这个:

<html>
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>  
</head> 
<body ng-app="myApp" ng-controller="myCtrl"> 

{{myDate | date: 'dd MMM yyyy'}}
<script>
//Module Declaration
var app = angular.module('myApp',[]);
//Controller Declaration
app.controller('myCtrl',function($scope){
    $scope.myDate="20140313T00:00:00";
});
</script> 
</body> 
</html> 

有关详细信息,请参阅此文档:https://docs.angularjs.org/api/ng/filter/date