Sunny Sunny - 1 year ago 99
AngularJS Question

AngularJS filter on date, display only time if year/month/day the same

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 ( for ending date. If they're different I want to display everything. Like this:

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

Any hints on how to do that?

Answer Source

Change your html to:

 {{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';
        return 'dd.MM.yyyy HH:mm';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download