jf3096/json-typescript-mapper

String date to Object Date

basvandenheuvel opened this issue · 1 comments

Hi,

From my backend i retrieve a date as a string, i would like to make it a Date object in my frontend. The following 2 options do NOT work:

@JsonProperty('date')
Date: Date;

@JsonProperty({clazz: Date, name: 'date'})
Date: Date;

Any ideas or is this not possible?

You should use a "customConverter" :

const dateConverter: ICustomConverter = {
    fromJson(data: any): any {
        return new Date(data);
    },

    toJson(data: any): any {
        return 'some-date';
    }
};
    @JsonProperty({name: 'dob', customConverter: dateConverter})
    dateOfBirth: Date = undefined;