AngularJS | date Filter

AngularJS date filter is used to convert a date into a specified format. When the date format is not specified, the default date format is ‘MMM d, yyyy’.

Syntax:

{{ date | date : format : timezone }}

Parameter Values: The date filter contains format and timezone parameters which is optional.



Some common values used in format are as follow:

  • ‘YYYY’ – define year ex. 2019
  • ‘YY’ – define year ex. 19
  • ‘Y’ – define year ex. 2019
  • ‘MMMM’ – define month ex. April
  • ‘MMM’ – define month ex. Apr
  • ‘MM’ – define month ex. 04
  • ‘dd’ – define day ex. 09
  • ‘d’ – define day ex. 9
  • ‘hh’ – define hour in AM/PM
  • ‘h’ – define hour in AM/PM
  • ‘mm’ – define minute
  • ‘m’ – define minute
  • ‘ss’ – define second
  • ‘s’ – define second

Some predifined values for format are as follow:

  • “short” – equivalent to “M/d/yy h:mm a”
  • “medium” – equivalent to “MMM d, y h:mm:ss a”
  • “shortDate” – equivalent to “M/d/yy” (5/7/19)
  • “mediumDate” – equivalent to “MMM d, y” (May 7, 2019)
  • “longDate” – equivalent to “MMMM d, y” (May 7, 2019)
  • “fullDate” – equivalent to “EEEE, MMMM d, y” (Tuesday, May 7, 2019)
  • “shortTime” – equivalent to “h:mm a” (2:35 AM)
  • “mediumTime” – equivalent to “h:mm:ss a” (2:35:05 AM)

Example 1: This example display the date in given format.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Date Filter</title>
          
        <script src=
        </script>
    </head>
      
    <body>
   
        <div ng-app="gfgApp" ng-controller="dateCntrl">
            <p>{{ today | date : "dd.MM.y" }}</p>
        </div>
   
        <script>
            var app = angular.module('gfgApp', []);
            app.controller('dateCntrl', function($scope) {
                $scope.today = new Date();
            });
        </script>
    </body>
</html>

chevron_right


Output:

07.05.2019

Example 2: This example display the time in specified format.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Date Filter</title>
          
        <script src=
        </script>
    </head>
      
    <body>
   
        <div ng-app="gfgApp" ng-controller="dateCntrl">
            <p>{{ today| date : 'mediumTime'}}</p>
        </div>
   
        <script>
            var app = angular.module('gfgApp', []);
            app.controller('dateCntrl', function($scope) {
                $scope.today = new Date();
            });
        </script>
    </body>
</html>

chevron_right


Output:

2:37:23 AM

Example 3: This example display the date in specified format.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Date Filter</title>
          
        <script src=
        </script>
    </head>
      
    <body>
   
        <div ng-app="gfgApp" ng-controller="dateCntrl">
            <p>{{ today| date }}</p>
        </div>
   
        <script>
            var app = angular.module('gfgApp', []);
            app.controller('dateCntrl', function($scope) {
                $scope.today = new Date();
            });
        </script>
    </body>
</html>

chevron_right


Output:

May 7, 2019


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.