9

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');
}
3
  • Why are you declaring var $scope outside your controller? And you have en error in the console, moment isn't included. Commented Feb 13, 2015 at 19:29
  • 2
    Add moment library to your project - jsfiddle.net/sed6x5e8/1 Commented Feb 13, 2015 at 19:30
  • 1
    I copied from internet. I am new to Angularjs. Commented Feb 13, 2015 at 19:30

3 Answers 3

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

Does Angular support localization?
10

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>

7 Comments

@user3842029 np. P.S. Not sure why the answer got down voted. Would the person care to explain?
Any chance you could elaborate on your answer? I'm not clear how to implement it. I want to {{getDate}} from the back end to display within ngrepeat but I can't put that between {{}} . . . {{{{getDate}}|date}} throws an error.
<span> {{ obj.get('Start') | date: 'shortDate'}}</span>.
I downvoted this. This functionality is built into angular using the date filter, there is absolutely no need to bring in another library to do it.
@ribsies well it seems like you're assuming that the OP's requirements stop at just formatting dates... moment.js has some other features that they might be using. I'm aware of angular's date filter but i just answered based on what was asked.
|
0

sample example

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.