使用管道将日期格式设置为dd/MM/yyyy [英] Format date as dd/MM/yyyy using pipes

查看:195
本文介绍了使用管道将日期格式设置为dd/MM/yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用date管道设置日期格式,但是如果没有解决方法,我只是无法获得所需的确切格式.我是错误地理解管道还是只是不可能?

I'm using the date pipe to format my date, but I just can't get the exact format I want without a workaround. Am I understanding pipes wrongly or is just not possible?

//our root app component
import {Component} from 'angular2/core'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <h3>{{date | date: 'ddMMyyyy'}}, should be 
      {{date | date: 'dd'}}/{{date | date:'MM'}}/{{date | date: 'yyyy'}}</h3>

    </div>
  `,
  directives: []
})
export class App {
  constructor() {
    this.name = 'Angular2'
    this.date = new Date();
  }
}

plnkr视图

推荐答案

管道日期格式错误已在Angular 2.0.0-rc.2,

Pipe date format bug fixed in Angular 2.0.0-rc.2, this Pull Request.

现在,我们可以按照常规方式进行操作:

Now we can do the conventional way:

{{valueDate | date: 'dd/MM/yyyy'}}


示例:

当前版本:

Angular 8.xx示例

旧版本:

Angular 7.x示例

示例Angular 6.x

示例Angular 4.x

示例Angular 2.x

文档DatePipe

这篇关于使用管道将日期格式设置为dd/MM/yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆