English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Date Formatting in Angularjs Controller (controller.js) Using Filters ($filter)/Time Example

Angularjs built-in filters (filter) provide powerful functions for formatting our data information, such as: formatting time, date, formatting number precision, language localization, formatting currency, etc. However, these filters are generally used in the VIEW, such as formatting time./The VIEW view code for dates:

<div ng-app>
  <p>
    <label>Select a date</label>/label>
    <input type="date" id="date" ng-model="datevalue" />
  </p>
  <p> {{ datevalue | date : 'fullDate'}} </p>
</div> 

Then the question arises, if I need to use the filter in the js code of the controller (controller) to format the time/How should the date be handled? Let's directly go 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');
    }
; 

The above is how to use the filter ($filter) in the js code of the controller (controller.js) to format the date/Implementation of time. Hope it will be helpful to everyone's learning, and also hope that everyone will support the Yelling Tutorial more.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like