在Angular 5.1中使用DatePipe,我需要以小写形式格式化句点字段类型(AM / PM)。根据documentation,
Tuesday, December 19, 7:00 am
应该
date:'EEEE, MMMM d, h:mm a'
但是,句点字段类型始终以大写形式显示,因此我看到:
Tuesday, December 19, 7:00 AM
我做错了什么或者这是Angular日期格式的已知缺陷?
您可以将日期分为两部分:
{{ today | date : 'EEEE, MMMM d, h:mm' }} {{ today | date : 'a' | lowercase }}
...............
UPDATE
这是另一种实现它的简单方法,使用内置的date
管道和aaaaa matcher(返回小写的a
或p
):
<div>{{ today | date : 'EEEE, MMMM d, h:mm aaaaa\'m\'' }}</div>
StackBlitz更新:https://stackblitz.com/edit/angular-dcpgzb?file=app%2Fapp.component.html
........
ANGULAR JS解决方案
app.controller('MainCtrl', function($scope, $locale) {
$locale.DATETIME_FORMATS.AMPMS = ['am', 'pm'];
$scope.today = new Date();
});
您可以在日期之后使用lowercase
过滤器将am / pm转换为小写。
For example: 1343623006 | date:'EEEE, MMMM d, h:mm a' | lowercase
游民。 Angular 5的情况仍然如此。
我创建了一个自定义管道,它将小写变换应用于与提供的正则表达式匹配的文本。
小写,match.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'lowercaseMatch'
})
export class LowerCaseMatchPipe implements PipeTransform {
transform (input: any, pattern: any): any {
if (!this.isString(input)) {
return input;
}
const regexp = pattern instanceof RegExp ? pattern : new RegExp(pattern, 'gi');
return input.toLowerCase()
if (input.match(regexp)) {
return input.toLowerCase()
}
return input
}
isString(value: any): boolean {
return typeof value === 'string';
}
}
导入到模块
import { LowerCaseMatchPipe } from './lowercase-match.pipe';
@NgModule({
declarations: [
...
LowerCaseMatchPipe
],
...
})
export class AppModule { }
以小写am / pm显示日期
{{ today | date : 'EEEE, MMMM d, h:mm a' | lowercaseMatch : 'am|pm' }}
在Angular https://github.com/angular/angular.js/issues/8763的GitHub问题上有一些关于这个套管概念的讨论