ngx-moment icon indicating copy to clipboard operation
ngx-moment copied to clipboard

How to handle null values ?

Open chaouiy opened this issue 7 years ago • 11 comments

Is theire a way to handle null values. {{date | amTimeAgo}} displays invalide date if date is null how customize that to something like --

chaouiy avatar Mar 30 '17 12:03 chaouiy

+1

leandrodiniz avatar Apr 10 '17 20:04 leandrodiniz

Is there any updates?

davidgabrichidze avatar Jun 10 '17 17:06 davidgabrichidze

Hi! I really like this pipe and I was wondering if there was any momentum to change how the pipe handles null, nil, or undefined dateTime values. Thanks!

lizdenhup avatar Dec 05 '17 23:12 lizdenhup

I'd love to hear a few suggestions so we can choose

urish avatar Dec 05 '17 23:12 urish

if date is null just return a null, by default, or return a parameter like {{ date | amTimeAgo:'--' }}. timeAgo has static format, so it is enough to have only null value format parameter.

davidgabrichidze avatar Dec 06 '17 05:12 davidgabrichidze

Any news on this? Would it be feasible to use the second parameter to define the default message for an invalid date?

That shouldn't be a breaking change, as the default message could still be Invalid date. I'm happy to work on an implementation, update the tests and send a PR for consideration.

bergarces avatar Jul 24 '18 20:07 bergarces

{{someItem.dueToUtc ? (someItem.dueToUtc | amFromUtc | amLocal | amDateFormat:'LL') : 'your message'}}

RouR avatar Dec 23 '18 18:12 RouR

Personally, I think we should add some condition the pipe to avoid condition on the template likes this pipe

export class DateFormatPipe implements PipeTransform {
  transform(value: Date | moment.Moment | string | number, ...args: any[]): string {
    if (!value) { return ''; }
    return momentConstructor(value).format(args[0]);
  }
}

hoang-innomize avatar Feb 21 '19 04:02 hoang-innomize

Why not copy the logic of the standard angular pipes when it comes to null values... https://github.com/angular/angular/blob/master/packages/common/src/pipes/date_pipe.ts

transform(value: any, format = 'mediumDate', timezone?: string, locale?: string): string|null {
    if (value == null || value === '' || value !== value) return null;

    try {
      return formatDate(value, format, locale || this.locale, timezone);
    } catch (error) {
      throw invalidPipeArgumentError(DatePipe, error.message);
    }
  }

melgish avatar Mar 11 '20 13:03 melgish

How about Never?

ghost avatar Nov 03 '20 13:11 ghost

I suggest you to create a new Pipe extending the original one, with something like this :

import { Pipe, PipeTransform } from '@angular/core';
import moment from 'moment';
import { TimeAgoPipe } from 'ngx-moment';

@Pipe({
  name: 'customTimeAgo'
})
export class CustomTimeAgoPipe extends TimeAgoPipe implements PipeTransform {

  transform(value: moment.MomentInput, omitSuffix?: boolean, formatFn?: (m: moment.Moment) => string): string {
    if (!value) {
      return '-';
    }
    return super.transform(value, omitSuffix, formatFn);
  }

}

... and use {{ myDate |customTimeAgo }} in your html template

msieurtoph avatar Jan 24 '22 15:01 msieurtoph