【问题标题】:Format Datetime according to the current language根据当前语言格式化日期时间
【发布时间】:2019-08-03 23:16:37
【问题描述】:

我想根据用户语言格式化UI上显示的日期,即:

  • 如果选择的用户语言是英语:08/07/2019
  • 当他改成德语时:07.08.2019

在积云中怎么可能?您是否有用于日期时间本地化的管道或某些功能?

【问题讨论】:

  • 我认为他们对此没有任何配置。顺便说一句,你在使用 Cockpit,一个基于 Cockpit 的应用程序吗?
  • 不,我使用 Angular SDK,但我猜它仍然没有驾驶舱。

标签: datetime localization cumulocity


【解决方案1】:

据我所知,在 c8y web sdk 中没有内置可感知区域设置的日期格式化程序。

但是由于它依赖于moment.js,所以编写自己的日期格式化管道应该不会太复杂。

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);
  }
}

如果您不想指定自定义日期格式模式,您始终可以使用moment.js 提供的默认格式之一。

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');

【讨论】:

    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    相关资源
    最近更新 更多