Leon Gaban
Leon Gaban

Reputation: 39034

How to just show 4 digit year in Angular?

I found the Angular Date documentation here, and thought the filter yyyy would be enough, but it's still attaching on the actual day.

My HTML:

<p class="footer_copy">&copy; {{ footer.date | date : yyyy }} AT&amp;T</p>

My Angular Directive for the footer, with a function to create a date:

// Directive for Footer
app.directive('appFooter', function () {
    return {
        restrict: 'E',
        templateUrl: 'templates/app-footer.html',
        controller: function(){
            this.date = Date.now();
        },
        controllerAs:'footer'
    };
});

However right now it spits out © Sep 24, 2014 AT&T

I'd like it to just create © 2014 AT&T

Upvotes: 1

Views: 1910

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388376

You need to use to pass the format as a string

var app = angular.module('my-app', [], function() {})

app.directive('appFooter', function () {
    return {
        restrict: 'E',
        template: '<p class="footer_copy">&copy; {{ footer.date | date:"yyyy" }} AT&amp;T</p>',
        controller: function(){
            this.date = Date.now();
        },
        controllerAs:'footer'
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
  <app-footer></app-footer>
</div>
Another option is to pass the format as a variable like
var app = angular.module('my-app', [], function() {})

app.directive('appFooter', function() {
  return {
    restrict: 'E',
    template: '<p class="footer_copy">&copy; {{ footer.date | date:footer.dateFormat }} AT&amp;T</p>',
    controller: function() {
      this.date = Date.now();
      this.dateFormat = 'yyyy'
    },
    controllerAs: 'footer'
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
  <app-footer></app-footer>
</div>

Upvotes: 3

Related Questions