Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output an empty string if date is '0000-00-00' in angular

Tags:

angularjs

The following line outputs the string "30.11.-0001" in case the date is "0000-00-00".

{{ payment.pay_date | date: 'dd.MM.yyyy' }}

How do I make it output a blank string in case the date is "0000-00-00"? (if possible, without using if clauses or directives)

like image 907
Tool Avatar asked Mar 19 '15 11:03

Tool


2 Answers

The code above has some syntax error. Please try this one. This is perfect and it worked for me very well.

((dtItem.DueDate !== '0001-01-01T00:00:00') ? (dtItem.DueDate | date: 'dd/MM/yyyy') : '' )
like image 171
Arpita Upadhyay Avatar answered Nov 15 '22 00:11

Arpita Upadhyay


use a custom filter , so its reusable in your app

for example,

app.filter('hideIfEmpty', function($filter) {
    return function (dateString, format) {
        if(dateString === '0000-00-00') {
            return "";
        } else {
            return $filter('date')(dateString, format.toString());
        }
    };
});

you can use this by

{{ payment.pay_date | hideIfEmpty: 'dd.MM.yyyy' }}

if date is equals to '0000-00-00' then return a empty string , or you can return something u like, if is not equals to '0000-00-00' then format the date according to the format and return.

here is the example fiddle (its not output something because filter return empty string "" use a correct date and test to check. :) )

like image 45
Kalhan.Toress Avatar answered Nov 15 '22 00:11

Kalhan.Toress