加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:19:46 所属栏目:安全 来源:网络整理
导读:需求中要对时间格式化的处理,处理成类似20180323的类型,在过滤器中有定义和引入了时间格式转化的方法: import {Pipe , PipeTransform} from '@angular/core' ; @Pipe({ name : 'datex' }) export class DatexPipe implements PipeTransform { transform (

需求中要对时间格式化的处理,处理成类似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的格式了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读