Set an AngularJS date filter format to be used by all references to the date filter?

前端 未结 5 950
余生分开走
余生分开走 2021-02-18 18:03

Rather that having to define a custom format for each call to the date filter, is there a way to globally define a default format (other than \'medium\'

5条回答
  •  不思量自难忘°
    2021-02-18 18:23

    In case you are working with .aspx pages and have the date format stored in a configuration file...

    1. From the .aspx that injects your Angular page, set a global variable with the web.config date format

      var _settingsDateFormat = '<%=appSettings.DateFormat%>';

    2. Set a global filter to be used through your app

    yourModule.filter('myDateFormat', function ($filter) {
            return function (input) {
                if (input == null) { return ""; }
                var formattedDate = $filter('date')(new Date(input), _settingsDateFormat);
                return formattedDate;
            };
    });
    1. Just add the custom filter to your dates
    
           Effective {{row.StartDate|myDateFormat}} - {{row.StopDate|myDateFormat}}
        

提交回复
热议问题