0

I want to format the dates displayed on UI according to the user language, i.e.:

  • If selected user language is English: 08/07/2019
  • And when he changes to German: 07.08.2019

How is it possible in Cumulocity? Do you have pipes or some functions for datetime localization?

Parda
  • 31
  • 5

1 Answers1

1

As far as I know there is no such thing as a locale aware date formatter built into c8y web sdk.

But since it depends on moment.js it should not be too complicated to write your own date formatter pipe.

import { Pipe, PipeTransform } from '@angular/core';
import { MomentInput } from 'moment';
import { TranslateService as NgxTranslateService } from '@ngx-translate/core';
import * as moment from 'moment';

@Pipe({ name: 'customdateformatter' })
export class MomentFormatPipe implements PipeTransform {
  constructor(private translateService: NgxTranslateService) {}

  transform(value: MomentInput, formatPattern: string): string {
    if (!value) {
      return '';
    }
    return moment(value)
      .locale(this.translateService.currentLang)
      .format(formatPattern);
  }
}

If you do not want to specify a custom date format pattern you can always use one of the default ones moment.js provides.

moment().format('LT');
moment().format('LTS');
moment().format('L');
moment().format('l');
moment().format('LL');
moment().format('ll');
moment().format('LLL');
moment().format('lll');
moment().format('LLLL');
moment().format('llll');