I'm receiving these kinds of values from API: "2019092300000000" as a date to be properly formated: into the "dd/mm/yyyy" format.

// The below line of code fetches the above value from API.
this.releaseDate= this.selectedNote.dataUno["0"].releaseDate;   

I'm trying to apply Angular date pipe transform, but I get a very weird date if I feed the pipe transform with that long string:

   this.datePipe.transform(
        this.releaseDate, "dd/mm/yyyy"
      )

this outputs this: 27/13/65952, which is wrong.

Then I tried to store the value in a variable and apply the slice method to remove the zeroes like this:

 this.releaseDateSliced = this.releaseDate.slice(0, 8); // this removes the zeroes.

and if I print this.fechaEmisionSliced in console, actually I get this 20190923 which seems to be a valid value to transform using angular date pipe.

But after applying the date pipe like this:

 this.datePipe.transform(
        this.fechaEmisionSliced,
        "dd/mm/yyyy"
      )

I get this date: 01/36/1970 which obviously is wrong.

How can I accomplish to properly format the date?

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Since this is not a proper timestamp of seconds/milliseconds, you should parse it as a string and just build the date.

Here is one way of doing it:

var dateStr = '2019092300000000';
var year = Number(dateStr.substring(0, 4))
var month = Number(dateStr.substring(4, 6)) - 1
var day = Number(dateStr.substring(6, 8)) + 1
var date = new Date(year, month, day);
console.log(date)