1

I have a working (in production) web app (material + angular 5 (5.2.11)). Also I've an API written in .dot core 2 (C#) using Nancy FX and newtonsoft json.

Problem:

DB (mariaDB running on Ubuntu Server): I have this value: 2018-05-16 20:42:36 on a record.

Calling the endpoint yields the correct JSON:

{"timestamp":"2018-05-16T20:42:36Z"}

(the other fields were removed for sanity)

On Angular app I use:

... return this._http.get(this.endpoint + '/' + uuid, { headers: this._getHeaders }).catch(this.handleError);

Where <T> represents a model that includes timedate: Date; as a property.

Using the service:

this._dataService.getByUuid(uuid).subscribe(result => { console.log(result); });

gives:

Object { timedate: "2018-05-16 08:05:36" }

So, the time lacks of AMPM format and I can't display it correctly. {{element.timedate | date: 'dd/MM/yyyy HH:mm' }} does nothing since timedate is just that, a bare string.

What have I tried:

Problem is with any datetime field. The JSON is always on point and so the database.

Any help is appreciate

Esselans
  • 1,540
  • 2
  • 24
  • 44

1 Answers1

1

JSON doesn't have a Date type (only arrays, numbers, string, object, null and undefined), so the converter from JSON to TypeScript cannot know whether it's a date or a plain string.

You need to parse (Date.Parse(yourString) or new Date(yourString)) the Date property everytime your object is deserialized.

** Date.Parse and the Date constructor can take in a Date as well as a string so you don't really have to type check the value before using them.*

Simon Corcos
  • 962
  • 14
  • 31