angular4在typeScript中怎么调用过滤器处理时间格式

需求中要对时间格式化的处理,处理成类似20180323的类型,在过滤器中有定义和引入了时间格式转化的方法:

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

@Pipe({ name: 'datex'})

export class DatexPipe implements PipeTransform { 

  transform(value: any, format: string = ""): string

  // Try and parse the passed value. 

moment.locale('zh-cn'); let momentDate = moment(value); 

// If moment didn't understand the value, return it unformatted. 

if (!momentDate.isValid()) return value; 

// Otherwise, return the date formatted as requested. 

return momentDate.format(format); }}

其中datex是提供的过滤器

怎么在typeScript中使用呢:引入DatexPipe的类,new出它的对象,然后调用它的transform方法

import {DatexPipe} from '../../global/pipe'
let datexPipe = new DatexPipe() ; let date = datexPipe. transform( this. match.matchTime , 'YYYYMMDD') ;

得到的date就是20180323的格式。

在html上怎么使用过滤器呢?

<div class="time">
    <img src="assets/images/ic_date@2x.png">
    {{match.matchTime | datex: 'YYYY-MM-DD HH:mm'}}
</div>
<div class="duration">
    <img src="assets/images/ic_time@2x.png">
    {{match.matchDuration|possessionTimeFilter}}
</div>
调用datex并补上时间格式,得到的就是2018-03-23 11:30的格式了。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值