Dumber_Texan2
Dumber_Texan2

Reputation: 978

How to convert Asp.Net Core DateTime to Angular Date using Constructor?

I am using Angular 8 with Asp.Net Core. The current problem I'm having is that I can not display the dates within a Modal using [(ngModel)] and a date pipe.

What I would like to do is to convert the Asp.Net Core DateTime into an Angular Date format in the constructor.

Here is my code. Created, StartDate and EndDate are all Date.

export class Content {
    Id: string;
    CompanyId: string;
    Created: Date; 
    Name: string;
    Url: string;
    Instructions: string;
    Tags: string; 
    StartDate: Date; 
    EndDate: Date;
    Deleted: Boolean;
    Pause: Boolean;
    Published: Boolean;

    constructor(contentResponse: any) {
        this.Id = contentResponse.id;
        this.CompanyId = contentResponse.companyId;
        this.Created = contentResponse.Created;
        this.Name = contentResponse.name;
        this.Url = contentResponse.url;
        this.Instructions = contentResponse.instructions;
        this.Tags = contentResponse.tags;
        this.StartDate = contentResponse.startDate;
        this.EndDate = contentResponse.endDate;
        this.Deleted = contentResponse.deleted;
        this.Pause = contentResponse.pause;
        this.Published = contentResponse.published;
    }

 }

From console.log, I can see the following Date formats.

JSON from API

created: "2019-08-03T21:26:51.7981863"

endDate: "2020-12-31T00:00:00"

startDate: "2019-07-15T00:00:00"

After conversion using current constructor

Created: undefined

EndDate: "2020-12-31T00:00:00"

StartDate: "2019-07-15T00:00:00"

Does anyone know how to convert this in the constructor so Angular can display this information and work with it?

Any help is much appreciated. Thanks!

Upvotes: 2

Views: 2392

Answers (1)

jspassov
jspassov

Reputation: 811

Try Moment JS in you angular app. I am using it with many angular projects with asp.net and asp.net core backend with no problem at all. If you want to use 'native' angular date pipe the only issue that you can found is localizing your data. But you can use moment also for this kind of things.

Hope it's help.

Upvotes: -1

Related Questions