AngularJS过滤日期,仅显示年/月/日相同的时间

时间:2022-08-22 19:42:46

I'm new to AngularJS, for the item I have beginning date and ending date. If the year/month/day are the same for both, I'd like to only display time (HH.mm) for ending date. If they're different I want to display everything. Like this:

我是AngularJS的新手,对于我有开始日期和结束日期的项目。如果两者的年/月/日相同,我只想显示结束日期的时间(HH.mm)。如果它们不同我想展示一切。喜欢这个:

{{item.bDate | date:'dd.MM.yyyy HH:mm'}} - {{item.eDate | date:'dd.MM.yyyy HH:mm' }}

Any hints on how to do that?

关于如何做到这一点的任何提示?

2 个解决方案

#1


4  

Change your html to:

将您的HTML更改为:

 {{item.bDate | date: getDateFormat(item.bDate, item.eDate) }}-
 {{item.eDate | date: getDateFormat(item.bDate, item.eDate) }}

and creates this function in your controller:

并在您的控制器中创建此功能:

$scope.getDateFormat = function(bDate,eDate){
    var beginDate = new Date(bDate);
    var endDate = new Date(eDate);

    if (beginDate.getDate() == endDate.getDate())
        return 'HH:mm';
    else
        return 'dd.MM.yyyy HH:mm';
}

#2


2  

You could create a new custom filter:

您可以创建一个新的自定义过滤器:

.filter('enddate', function($filter){

    var filterDate = $filter('date');

    function datesEqual(endDate, startDate){
        return endDate.getDate() == startDate.getDate() &&
            endDate.getMonth() == startDate.getMonth() &&
            endDate.getYear() == startDate.getYear();
    }

    return function(endDate, startDate){
        return datesEqual(endDate,startDate) ? filterDate(endDate, 'HH:mm') : filterDate(endDate, 'dd.MM.yyyy HH:mm');
    };
})

View:

视图:

{{item.bDate | date:'dd.MM.yyyy HH:mm' }} - {{item.eDate | enddate: item.bDate}}

Fiddle

小提琴

#1


4  

Change your html to:

将您的HTML更改为:

 {{item.bDate | date: getDateFormat(item.bDate, item.eDate) }}-
 {{item.eDate | date: getDateFormat(item.bDate, item.eDate) }}

and creates this function in your controller:

并在您的控制器中创建此功能:

$scope.getDateFormat = function(bDate,eDate){
    var beginDate = new Date(bDate);
    var endDate = new Date(eDate);

    if (beginDate.getDate() == endDate.getDate())
        return 'HH:mm';
    else
        return 'dd.MM.yyyy HH:mm';
}

#2


2  

You could create a new custom filter:

您可以创建一个新的自定义过滤器:

.filter('enddate', function($filter){

    var filterDate = $filter('date');

    function datesEqual(endDate, startDate){
        return endDate.getDate() == startDate.getDate() &&
            endDate.getMonth() == startDate.getMonth() &&
            endDate.getYear() == startDate.getYear();
    }

    return function(endDate, startDate){
        return datesEqual(endDate,startDate) ? filterDate(endDate, 'HH:mm') : filterDate(endDate, 'dd.MM.yyyy HH:mm');
    };
})

View:

视图:

{{item.bDate | date:'dd.MM.yyyy HH:mm' }} - {{item.eDate | enddate: item.bDate}}

Fiddle

小提琴