关于angular2通道使用ts

需求在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]
})

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 大家好,请关注我的新作 multipages-generator今日发布👏!妈妈再也不用担心移动端h5网站搭建了!...
    yitalalww阅读 67,698评论 72 124
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,117评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • Collecting Data with Forms and Validation Check code at: ...
    wghglory阅读 353评论 0 0
  • 就在昨天,我才发现我丢了我最心爱的水杯,乒乓球拍,和我的数学笔记,虽然不是很贵,但是他们是我的心血,我的亲近...
    晚安WANAN阅读 210评论 0 0