cobolstinks
cobolstinks

Reputation: 7143

Angular2 Date filter C# Date?

Is there a built in Date pipe filter to handle dates the way the default asp.net JSON serializer, serializes dates?

Here is the example format: '/Date(1466624402557)/'

Otherwise I can write a custom pipe.

Per rinukkusu here is the custom pipe i've created:

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 );
    }
}

But am getting this error:

platform-browser.umd.js:962 ORIGINAL EXCEPTION: Invalid argument '1466624402557' for pipe 'DatePipe'

I've tried boxing the sliced string to a new Date() but that doesn't work either...

Upvotes: 0

Views: 1212

Answers (3)

Mostafa Fateen
Mostafa Fateen

Reputation: 847

To the extent of my knowledge, No. You can refer to the API of the date pipe in Angular so you'll have to implement it yourself


Update:

According to the Changelog the numeric date one of the features added is " datePipe: numeric string support " you can see the related commit here

Upvotes: 1

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

You can leverage the built-in pipes slice and date to achieve this:

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

Otherwise go for the custom pipe route. You can use it like the date pipe argumentwise:

@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);
    }
}

In your template for instance:

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

Plunker for example usage

Upvotes: 2

null canvas
null canvas

Reputation: 10613

Here is the DatePipe, you can directly check what it supports or not. Currently no support for that format:

https://github.com/angular/angular/blob/5c8d3154d755f879d328739b78952fc88d38681f/modules/%40angular/common/src/pipes/date_pipe.ts

Upvotes: 1

Related Questions