设置一个 AngularJS 日期过滤器格式以供对日期过滤器的所有引用使用?

问题描述:

而不是为每次调用 date 过滤器定义自定义格式,有没有办法全局定义默认格式(除了 'medium')?

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')?

我希望将格式设置在一个文件中,而不是在我的代码中到处设置(日期格式将来可能最终会更改,并且在一个文件中进行修改比必须进行更改要好得多在许多文件中).

I would like to have the format set in one file rather than all over the place in my code (the date format may end up being changed in the future and a modification in one file would be much nicer than having to make changes in many files).

这就是我现在正在做的事情(每次都定义日期格式):

This is what I am doing now (defining date format each time):

{{systemTime | date:'dd MMMM @ HH:mm:ss'}}
{{modifiedTime | date:'dd MMMM @ HH:mm:ss'}}
{{getShippedTime() | date:'dd MMMM @ HH:mm:ss'}}
   etc.

我正在考虑创建一个自定义过滤器(我们称之为 myDate),它将充当包装器,然后将日期字符串传递给 Angular date 过滤器使用我的自定义格式,那么我的代码可能看起来像这样:

I was thinking of creating a custom filter (let's call it myDate), which would act as a wrapper and then pass off the date string to the Angular date filter with my custom format, so then my code could just look like this:

{{systemTime | myDate}}
{{modifiedTime | myDate}}
{{getShippedTime() | myDate}}
   etc.

但是,我不知道如何让 myDate 过滤器指向 Angular date 过滤器.

However, I can't figure out how to make the myDate filter point to the Angular date filter.

感谢您的帮助.

基于更多的研究,然后考虑到 Moderndegree 的评论,我发现以下 myDate 过滤器将起作用:>

Based on some more research and then considering the comment by moderndegree, I have found that the following myDate filter will work:

.filter('myDate', function($filter) {    
    var angularDateFilter = $filter('date');
    return function(theDate) {
       return angularDateFilter(theDate, 'dd MMMM @ HH:mm:ss');
    }
});