2

I want to calculate the number of days after the difference between 2 dates (Both dates in isoString format). When I try to subtract it returns NaN instead of the days. Here is my code, may I know where I went wrong?

my HTML

<ion-datetime displayFormat="DD-MM-YYYY" done-text="Select" min="2020" max="2021" placeholder="Forward Date" (ionChange)="selectDate($event)"></ion-datetime>

my .ts file

 selectDate(value){
    this.nextDate= new Date(+new Date() + 86400000+ 86400000).toISOString();  // date after 2 days from current date
    this.check=value.detail.value;
    console.log(this.check) // 2020-06-29T12:01:57.100+05:30 (This date is obtained from ion dateTime picker which is already in toISOString format)

    console.log(this.nextDate) // 2020-06-25T07:02:22.513Z
    this.ihe=this.check-this.nextDate; // Gives Nan
    console.log(Math.round(this.ihe/ (1000 * 3600 * 24)))
  }

enter image description here

Here nextDate is predefined date and check variable contains date selected from date picker.

Ravi Shah
  • 105
  • 1
  • 11
  • 1
    You cannot subtract strings, you need to transform them back to `Date` objects – Greedo Jun 23 '20 at 07:21
  • Does this answer your question? [How do I get the difference between two Dates in JavaScript?](https://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript) – user120242 Jun 23 '20 at 07:23
  • Tried that but it didn't seem to work @user120242 – Ravi Shah Jun 23 '20 at 07:25
  • `this.ihe=new Date(this.check)-new Date(this.nextDate);` – user120242 Jun 23 '20 at 07:28
  • Also dupe of this: https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript – user120242 Jun 23 '20 at 07:28
  • I did try your above solution as well but my editor seems to have an issue with it, it says: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362) – Ravi Shah Jun 23 '20 at 07:30

2 Answers2

3

https://stackblitz.com/edit/angular-ivy-byvklo?embed=1&file=src/app/test/test.component.ts

this.ihe=Date.parse(this.check)- Date.parse(this.nextDate);
Sufyan Shaikh
  • 94
  • 1
  • 6
1

You try to calculate two strings. That's why you get a NaN. You should transform the two dates to integers. For example:

selectDate(value){
    this.nextDate= new Date(+new Date() + 86400000+ 86400000).toISOString();  // date after 2 days from current date
    this.check=value.detail.value;
    console.log(this.check) // 2020-06-29T12:01:57.100+05:30 (This date is obtained from ion dateTime picker which is already in toISOString format)

    console.log(this.nextDate) // 2020-06-25T07:02:22.513Z
    this.ihe=new Date(this.check).getTime()-new Date(this.nextDate).getTime(); // Gives Nan
    console.log(Math.round(this.ihe/ (1000 * 3600 * 24)))
}

In this example, you transform the date string on the fly to a Date Object on which you can use getTime(). By that, you get the delay between 1.1.1970 and the Date in milliseconds.

Nico Schuck
  • 832
  • 3
  • 15
  • 32