Reputation: 215
I am trying to format date using moment in Angularjs but its not working for me. Here is my fiddle http://jsfiddle.net/sed6x5e8/ and below is my code.
HTML:
<div ng-app="miniapp">
<div ng-controller="Ctrl">
<div>
Actual Date: {{date}}
<br><br>
Formatted Date: {{formattedDate}}
</div>
</div>
</div>
JS:
var $scope;
var app = angular.module('miniapp', [])
function Ctrl($scope) {
$scope.date = '2/13/2015';
$scope.formattedDate = moment($scope.date).format('YYYY-MM-DD');
}
Upvotes: 9
Views: 47092
Reputation: 23
var app = angular.module("app", []);
app.constant("moment", moment);
app.controller("ctrl", function($scope, moment) {
$scope.date = new moment().format("D/MMM/YYYY");
var dat1 = new moment();
$scope.date3 = dat1.add('5', 'd').format('MMMM Do YYYY, h:mm:ss a');
});
Html
var app = angular.module("app", []);
app.constant("moment", moment);
app.controller("ctrl", function($scope, moment) {
$scope.date = new moment().format("D/MMM/YYYY");
var dat1 = new moment();
$scope.date3 = dat1.add('5', 'd').format('MMMM Do YYYY, h:mm:ss a');
});
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
{{ date }}
</br>
</br>
{{date3}}
</div>
Upvotes: 0
Reputation: 1218
AngularJS has a built in filter for dates. You do not need to use moment, waste of resources.
<span>{{message.time | date}}</span>
https://docs.angularjs.org/api/ng/filter/date
Upvotes: 14
Reputation: 1600
I've used angular-moment successfully in a project of mine. It has a filter that alows you to format the date. Example from the README:
<span>{{message.time | amDateFormat:'dddd, MMMM Do YYYY, h:mm:ss a'}}</span>
Upvotes: 10