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)))
}
Here nextDate is predefined date and check variable contains date selected from date picker.
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.