-1

I'm working on an Angular webui project. Currently I have to get a value from a data-document.json which is of type date but I need to display it as a string. How would I be able to achieve this?

"FACD_Dispatch_DateTime": "2022-01-19T16:46:03.959",

this is the way that I've been trying:

map(([d]) => {

           let myDate: string;
          if (d?.value) {
            myDate = d.value;
          }

but terminal returns the error: Type 'Date' is not assignable to type 'string'

  • 1
    Does this answer your question? [Parsing ISO 8601 date in Javascript](https://stackoverflow.com/questions/4829569/parsing-iso-8601-date-in-javascript) – Liam Feb 11 '22 at 09:37

1 Answers1

0

well "FACD_Dispatch_DateTime": "2022-01-19T16:46:03.959" is not a Date, but a string. Anyway:

let myDate: Date;
if (d?.value) {
  myDate = d.value;
}

then you can display it in your HTML:

<h1> Day: {{myDate | date:'dd.MM.yyyy'}} Time: {{myDate | date:'HH:mm'}}</h1>
Zerotwelve
  • 2,087
  • 1
  • 9
  • 24
  • This is the error that I get from terminal when I try to retrieve it as is: Type 'Date' is not assignable to type 'string' – DatGuyRofi Feb 11 '22 at 10:20
  • so i edited my answer. anyway myDate must be of type Date `let myDate:Date`... but what is `d`? try to `console.log(d)` – Zerotwelve Feb 11 '22 at 10:41