Angular2日期过滤器C#日期



是否有一个内置的日期管道过滤器来处理默认ASP.NET JSON Serialializer的日期,序列化日期?

这是示例格式:'/date(1466624402557)/'

否则我可以写一个自定义管道。

per rinukkusu这是我创建的自定义管道:

import {PipeTransform, Pipe} from '@angular/core';
import {SlicePipe, DatePipe} from '@angular/common';
@Pipe({
    name: 'aspDate'
})
export class AspDatePipe implements PipeTransform {
    transform(value: string, arg: string): any {
        let slicedValue = new SlicePipe().transform(value, 6, -2);
        return new DatePipe().transform(slicedValue, arg );
    }
}

但是正在遇到此错误:

platform-browser.umd.js:962原始异常:无效参数'146662440257'for Pipe'date-pipe'

我尝试将切片字符串拳击到新的date(),但这也不起作用...

您可以利用内置的管道切片和日期来实现这一目标:

{{ '/Date(1466624402557)/' | slice:6:-2 | date }}

否则选择自定义管道路线。您可以像参数一样使用date管道:

@Pipe({
    name: 'aspDate'
})
export class AspDatePipe implements PipeTransform {
    transform(value: string, arg: string):any {
        let slicedValue = new SlicePipe().transform(value, 6, -2);
        return new DatePipe().transform(new Date(parseInt(slicedValue)), arg);
    }
}

例如,在模板中:

{{ '/Date(1466624402557)/' | aspDate:'fullDate' }}

plunker例如用法

这是DatePipe,您可以直接检查其支持是否支持。目前不支持该格式:

https://github.com/angular/angular/blob/5c8d3154d7555f879d328739b78952fc888681f/modules/@angular/common/common/common/src/src/pipes/dape_pipe.pepe.ppipe.ts

在我的知识范围内,不。


更新:

根据变形值的数字日期,添加的功能之一是"date -pipe:数字字符串支持&quot"您可以在此处查看相关的提交

最新更新