需求在ts 中需要把时间统一转换成2000-01-02 11:00:56
使用angular2自带通道 DatePipe
//app.component.ts
import { DatePipe } from '@angular/common';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
//或者在app.module.ts文件中引入
providers:[DatePipe]
})
export class AppComponent implements OnInit {
date:Date = new Date();
constructor(private datepipe:DatePipe ) {
this.date = this.datepipe.transform(this.date,"yyyy-MM-dd HH:mm:ss");
console.log(this.date);
}
}
提示不想在component中写就在module中填写
@NgModule({
declarations: [
AppComponent
],
imports: [
HttpModule,
SharedModule.forRoot(),
],
providers: [DatePipe],
bootstrap: [AppComponent]
})