File

src/app/modules/workspace/pipes/date-filter-xtimeAgo/date-filter-xtime-ago.pipe.ts

Description

Pipe for date format

Metadata

Methods

transform
transform(value: Date | string | number, format: string)

To create date format pipe

Parameters :
Name Type Optional Description
value Date | string | number No

current Date, string or number

format string No

format of Date

Returns : string
import { Pipe, PipeTransform } from '@angular/core';

import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);

/**
 * Pipe for date format
 */
@Pipe({
  name: 'fromNow'
})
export class DateFilterXtimeAgoPipe implements PipeTransform {
  /**
   * To create date format pipe
   * @param {Date} value current Date, string or number
   * @param {string} format format of Date
   */
  transform(value: Date | string | number, format: string): string {
      const local = dayjs(value).format('YYYY-MM-DD HH:mm:ss');
      if (dayjs(local).isValid()) {
        return dayjs(local).fromNow();
      } else {
        return 'Invalid date';
      }
  }
}

results matching ""

    No results matching ""