使用管道格式化日期为dd/MM/yyyy

Jp_*_*Jp_ 224 date angular-pipe date-pipe angular

我正在使用date管道格式化我的日期,但我无法获得我想要的确切格式而无需解决方法.我是错误地理解管道还是不可能?

//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();
  }
}
Run Code Online (Sandbox Code Playgroud)

plnkr视图

Fer*_*eal 385

管道日期格式错误已在Angular 2.0.0-rc.2中修复,此Pull请求.

现在我们可以做传统方式:

{{valueDate | date: 'dd/MM/yyyy'}}
Run Code Online (Sandbox Code Playgroud)

例子:

当前版本:

Plunker Angular 6.x.x


旧版本:

Plunker Angular 2.x

Plunker Angular 4.x


更多信息在文档中 DatePipe


Pra*_*nth 79

从angular/common导入DatePipe然后使用以下代码:

var datePipe = new DatePipe();
    this.setDob = datePipe.transform(userdate, 'dd/MM/yyyy');
Run Code Online (Sandbox Code Playgroud)

其中 userdate 将是你的日期字符串.看看这是否有帮助.

记下日期和年份的小写:

d- date
M- month
y-year
Run Code Online (Sandbox Code Playgroud)

编辑

您必须以locale最新的角度将字符串作为参数传递给DatePipe.我已在角度4.x进行了测试

例如:

var datePipe = new DatePipe('en-US');
Run Code Online (Sandbox Code Playgroud)

  • 使用Angular 2.1.1时,抛出此错误`提供的参数与调用目标的任何签名都不匹配.在`new DatePipe()上 (7认同)
  • 你可以使用像`new DatePipe('en-US');`这样的东西 (6认同)

Pra*_*obh 18

您可以通过简单的自定义管道来实现此目的.

import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';

@Pipe({
    name: 'dateFormatPipe',
})
export class dateFormatPipe implements PipeTransform {
    transform(value: string) {
       var datePipe = new DatePipe("en-US");
        value = datePipe.transform(value, 'dd/MM/yyyy');
        return value;
    }
}


{{currentDate | dateFormatPipe }}
Run Code Online (Sandbox Code Playgroud)

使用自定义管道的好处是,如果您希望将来更新日期格式,您可以更新自定义管道,它将反映每个地方.

自定义管道示例


小智 13

当我因任何原因需要使用日期时,我总是使用Moment.js.

试试这个:

import { Pipe, PipeTransform } from '@angular/core'
import * as moment from 'moment'

@Pipe({
   name: 'formatDate'
})
export class DatePipe implements PipeTransform {
   transform(date: any, args?: any): any {
     let d = new Date(date)
     return moment(d).format('DD/MM/YYYY')

   }
}
Run Code Online (Sandbox Code Playgroud)

在视图中:

<p>{{ date | formatDate }}</p>
Run Code Online (Sandbox Code Playgroud)

  • `moment`库对于像格式这样的小​​作业来说太大了! (6认同)

Dee*_*pak 12

我正在使用这个临时解决方案:

import {Pipe, PipeTransform} from "angular2/core";
import {DateFormatter} from 'angular2/src/facade/intl';

@Pipe({
    name: 'dateFormat'
})
export class DateFormat implements PipeTransform {
    transform(value: any, args: string[]): any {
        if (value) {
            var date = value instanceof Date ? value : new Date(value);
            return DateFormatter.format(date, 'pt', 'dd/MM/yyyy');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


R M*_*hed 8

角度:8.2.11

<td>{{ data.DateofBirth | date }}</td>
Run Code Online (Sandbox Code Playgroud)

输出: 1973 年 6 月 9 日

<td>{{ data.DateofBirth | date: 'dd/MM/yyyy' }}</td>
Run Code Online (Sandbox Code Playgroud)

输出: 09/06/1973

<td>{{ data.DateofBirth | date: 'dd/MM/yyyy hh:mm a' }}</td>
Run Code Online (Sandbox Code Playgroud)

输出: 09/06/1973 12:00 AM


Yus*_*hin 7

您可以在此处找到有关日期管道的更多信息,例如格式。

如果你想在你的组件中使用它,你可以简单地做

pipe = new DatePipe('en-US'); // Use your own locale
Run Code Online (Sandbox Code Playgroud)

现在,您可以简单地使用它的转换方法,该方法将是

const now = Date.now();
const myFormattedDate = this.pipe.transform(now, 'short');
Run Code Online (Sandbox Code Playgroud)


小智 7

您必须将语言环境字符串作为参数传递给 DatePipe。

var ddMMyyyy = this.datePipe.transform(new Date(),"dd-MM-yyyy");
Run Code Online (Sandbox Code Playgroud)

预定义格式选项:

1.      'short': equivalent to 'M/d/yy, h:mm a' (6/15/15, 9:03 AM).
2.      'medium': equivalent to 'MMM d, y, h:mm:ss a' (Jun 15, 2015, 9:03:01 AM).
3.      'long': equivalent to 'MMMM d, y, h:mm:ss a z' (June 15, 2015 at 9:03:01 AM GMT+1).
4.      'full': equivalent to 'EEEE, MMMM d, y, h:mm:ss a zzzz' (Monday, June 15, 2015 at 9:03:01 AM GMT+01:00).
5.      'shortDate': equivalent to 'M/d/yy' (6/15/15).
6.      'mediumDate': equivalent to 'MMM d, y' (Jun 15, 2015).
7.      'longDate': equivalent to 'MMMM d, y' (June 15, 2015).
8.      'fullDate': equivalent to 'EEEE, MMMM d, y' (Monday, June 15, 2015).
9.      'shortTime': equivalent to 'h:mm a' (9:03 AM).
10. 'mediumTime': equivalent to 'h:mm:ss a' (9:03:01 AM).
11. 'longTime': equivalent to 'h:mm:ss a z' (9:03:01 AM GMT+1).
12. 'fullTime': equivalent to 'h:mm:ss a zzzz' (9:03:01 AM GMT+01:00).
Run Code Online (Sandbox Code Playgroud)

在 app.component.module.ts 中添加 datepipe

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {DatePipe} from '@angular/common';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [
    DatePipe
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)


ULL*_*S K 6

如果有人在找时间和时区,这是给你的

 {{data.ct | date :'dd-MMM-yy h:mm:ss a '}}
Run Code Online (Sandbox Code Playgroud)

在日期和时间格式的末尾为时区添加z

 {{data.ct | date :'dd-MMM-yy h:mm:ss a z'}}
Run Code Online (Sandbox Code Playgroud)


Ahm*_*san 6

如果有人可以在角度 6 中以 AM 或 PM 的时间显示日期,那么这适合您。

{{date | date: 'dd/MM/yyyy hh:mm a'}}
Run Code Online (Sandbox Code Playgroud)

输出

在此处输入图片说明

预定义格式选项

示例在 en-US 语言环境中给出。

'short': equivalent to 'M/d/yy, h:mm a' (6/15/15, 9:03 AM).
'medium': equivalent to 'MMM d, y, h:mm:ss a' (Jun 15, 2015, 9:03:01 AM).
'long': equivalent to 'MMMM d, y, h:mm:ss a z' (June 15, 2015 at 9:03:01 AM GMT+1).
'full': equivalent to 'EEEE, MMMM d, y, h:mm:ss a zzzz' (Monday, June 15, 2015 at 9:03:01 AM GMT+01:00).
'shortDate': equivalent to 'M/d/yy' (6/15/15).
'mediumDate': equivalent to 'MMM d, y' (Jun 15, 2015).
'longDate': equivalent to 'MMMM d, y' (June 15, 2015).
'fullDate': equivalent to 'EEEE, MMMM d, y' (Monday, June 15, 2015).
'shortTime': equivalent to 'h:mm a' (9:03 AM).
'mediumTime': equivalent to 'h:mm:ss a' (9:03:01 AM).
'longTime': equivalent to 'h:mm:ss a z' (9:03:01 AM GMT+1).
'fullTime': equivalent to 'h:mm:ss a zzzz' (9:03:01 AM GMT+01:00).
Run Code Online (Sandbox Code Playgroud)

参考链接


Joh*_*uet 5

唯一对我有用的东西是从这里得到启发的:https//stackoverflow.com/a/35527407/2310544

对于纯dd / MM / yyyy,这对我有用,角度2 beta 16:

{{ myDate | date:'d'}}/{{ myDate | date:'MM'}}/{{ myDate | date:'y'}}
Run Code Online (Sandbox Code Playgroud)


Nik*_*hil 5

在 MacOS 和 iOS 上的 Safari 浏览器的 Angular 2 和 Typescript 中,日期管道无法正常运行。我最近遇到了这个问题。我不得不在这里使用 moment js 来解决这个问题。简而言之提及我所做的事情......

  1. 在您的项目中添加 momentjs npm 包。

  2. 在xyz.component.html下,(这里注意startDateTime是string数据类型)

{{ convertDateToString(objectName.startDateTime) }}

  1. 在 xyz.component.ts 下,

import * as moment from 'moment';

convertDateToString(dateToBeConverted: string) {
return moment(dateToBeConverted, "YYYY-MM-DD HH:mm:ss").format("DD-MMM-YYYY");
}
Run Code Online (Sandbox Code Playgroud)