Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date using moment Angularjs

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');
}
like image 627
user3842029 Avatar asked Feb 13 '15 19:02

user3842029


2 Answers

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

like image 86
ribsies Avatar answered Oct 08 '22 06:10

ribsies


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>
like image 39
jsonmurphy Avatar answered Oct 08 '22 05:10

jsonmurphy