Tool Tool - 1 year ago 58
AngularJS Question

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

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)

Answer Source

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. :) )

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download