Typescript array element Date not working

521 views Asked by At

I am trying to compare various Date elements like this one 2 date elements that represent date formats but not being able to compare both cause it is displayed that "core.js:6237 ERROR TypeError: newticketList.createdAt.toDateString is not a function". And this as well with any other function like getDate or getMonth ps.: I already searched that it can be not recognized as a Date format, but in this case how can I go around it?

 ticketList: Array<Ticket>;
let dateTime = new Date()

(...)

this.ticketList = this.ticketList.filter(
      newticketList => (newticketList.createdAt.toDateString() != dateTime.toDateString()))
2

There are 2 answers

0
Pedro On BEST ANSWER

I got it done. although both vars are set as Date inside they were being handled as String So the solution was instead of:

newticketList => 
        (newticketList.createdAt.toDateString() != dateTime.toDateString()))

We get this:

newticketList => 
        (new Date(newticketList.createdAt).toDateString() != dateTime.toDateString()))
1
Björn Hjorth On

createdAt does no contain a method called toDateString

this.ticketList.map(
  newticketList => console.log(newticketList.createdAt));

And check what you get out