2

I am using angular 8 application and I have this function:


 checkExpired(echeq: EcheqSubmissionApi) {
    const today = Date.now();
    const validUntil = echeq.validUntilUtc;
    if (validUntil < today) {
      echeq.status = EcheqSubmissionStatus.EXPIRED;
      return true;
    }
    return false;
  }

and

   validUntilUtc?: Date;

But I get this error:

Operator '<' cannot be applied to types 'Date' and 'number'.ts(2365)

on this line:

if (validUntil < today) {

How to correct this?

  • if (validUntil < today `)` { try – Swoox Mar 03 '20 at 14:42
  • ?? Dont understand what you mean –  Mar 03 '20 at 14:43
  • change `if (validUntil < today { ` into `if (validUntil < today ) {` add the `)` – Swoox Mar 03 '20 at 14:44
  • Ah, come on. That was type. I already corrected. But that is not the issue –  Mar 03 '20 at 14:45
  • 1
    Does this answer your question? [How do I get the current date in JavaScript?](https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript) – Ivar Mar 03 '20 at 14:46
  • 2
    @codeFreak [`Date.now()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now) returns the number of milliseconds elapsed since January 1, 1970. That is a number. Use `new Date()` to get the current date as a `Date` object. – Ivar Mar 03 '20 at 14:47
  • The error seems quite clear. You're trying to compare a date and a number. So, either convert the date to a number or vice versa. – VLAZ Mar 03 '20 at 14:48

2 Answers2

10

Using Date.now() returns a type of number thus the type inconsistency. Try converting your validUntil to the millisecond representation of that date and then compare to today.

ehutchllew
  • 940
  • 8
  • 14
0

Hint1:- try casting date something like this

const validUntil = new Date(echeq.validUntilUtc); 

Hint2:- or u can directly change the type of validUntilUtc to Date

validUntilUtc : Date;

now the operator shld work in below case

if (validUntil < today) {
....
}
Nemanja Todorovic
  • 2,521
  • 2
  • 19
  • 30
Asif rocky
  • 21
  • 2