Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the current year in Angular 1 using just {{ }}?

In a fairly complicated angular web app, I need the dynamic year to show up in a footer that doesn't have a directive. Is there an angular way to do it using just {{SomeDateFunction()}}?

I've seen people say {{Date.now() | date:'yyyy'}}, which doesn't display anything. I've tried {{new Date().getFullYear()}} which breaks angular and the error says

Syntax Error: Token 'Date' is an unexpected token at column 5 of the expression [new Date().getFullYear()] starting at [Date().getFullYear(].

I don't want to link a scope from the root scope, or put this in a directive just for the year, this is exactly the kind of thing {{ }} expressions should be able to solve, and non-date related simple {{ }} expressions display just fine.

like image 409
Bronanaza Avatar asked Jan 05 '17 17:01

Bronanaza


People also ask

How to fetch current year in angular?

Using new Date() constructor The new Date() constructor contains a getFullYear() method, which returns the current year in four-digit (2020) format according to the user local time. Here is an example, that renders the current year in the footer component of our angular app.

How do you get the current year in TypeScript?

To get the current year in TypeScript: Call the new Date() constructor to get a date object for the current date. Call the getFullYear() method on the date object. The getFullYear method will return a number that represents the current year.

How do you find the current year?

To get the current year, you can call the getFullYear() method will return the year of the specified Date according to local time. The value returned by is an absolute number. For the dates between 1000 and 9999 years, the getFullYear() method returns a four-digit number.

How can we get the current year in JavaScript?

To get the current year in JavaScript, call the new Date() constructor to get a date object and call the getFullYear() method on the result, e.g. new Date(). getFullYear() . The getFullYear method will return a number corresponding to the current year.


2 Answers

You need to define the date object inside the controller onto the scope.

$scope.date = new Date();

And then in view do

{{date| date:'yyyy'}}
like image 93
squiroid Avatar answered Sep 18 '22 01:09

squiroid


Its pretty late to response and hopefully you have already got the answer. Giving the answer for everybody's sake, -

(function(angular) {
  'use strict';
  var app = angular.module("myApp", []);
  app.controller('dateController', function($scope) {
    $scope.SomeDateFunction = function() {
      var currentdate = new Date();
      return currentdate.getFullYear();
    }
  });
})(window.angular);
<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="1.6.4" src="https://code.angularjs.org/1.6.4/angular.min.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="myApp">
  <div ng-controller="dateController">
    <h1>Hello World!</h1> &copy; this is copyright protected as of {{SomeDateFunction()}}
  </div>
</body>

</html>

it should get you what you want.

like image 42
pasaban Avatar answered Sep 20 '22 01:09

pasaban