Angularjs Formatting Date and Time Example with Filter of $filter in Controller of controller. js

  • 2021-07-22 08:30:39
  • OfStack

Angularjs built-in filter (filter) for our data information formatting provides more powerful functions, such as: formatting time, date, formatting digital precision, language localization, formatting currency and so on. However, these filters 1 are generally used in VIEW, such as the VIEW view code formatting the time/date:


<div ng-app>

  <p>

    <label>Select a date</label>

    <input type="date" id="date" ng-model="datevalue" />

  </p>

  <p> {{ datevalue | date : 'fullDate'}} </p>

</div> 

Then the question arises, what should I do if I need to use filter to format the time/date in the js code of the controller (controller)? Go straight to the code: View (view) template code:


<div ng-app="dateApp" ng-controller="dateController">

  <p> {{ result }} </p>

</div> 

Controller (controller) code:


var app = angular.module('dateApp', []);

  app.controller(

    'dateController',

    function ($scope, $filter) {

      $scope.result = $filter('date')(new Date(), 'fullDate');

    }

); 

Related articles: