Dates and Ionic Storage

I am not a big fan of the stock Angular date pipe, because it relies on the very wobbly JavaScript i18n language support. Here is the date pipe I use:

import {Injectable, Pipe, PipeTransform} from '@angular/core';
import {TranslateService} from "@ngx-translate/core";
declare function require(name:string): any;
let format = require('date-fns/format');

@Pipe({
  name: 'dateformat'
})
@Injectable()
export class DateFormatPipe implements PipeTransform {
  private format: string;

  constructor(xlator: TranslateService) {
    xlator.stream('dateFormat').subscribe(fmt => this.format = fmt);
  }

  transform(d: Date | string, xfmt: string): string {
    let fmt = xfmt || this.format;
    let rv = format(d, fmt);
    return rv;
  }
}

If no explicit format parameter is provided, it uses whatever the current localization has for dateFormat.

2 Likes