我想计算两个日期(两个日期均为 isoString 格式)之间的差异后的天数。当我尝试减去时,它返回 NaN 而不是天数。这是我的代码,我可以知道我错在哪里吗?
我的 HTML
<ion-datetime displayFormat="DD-MM-YYYY" done-text="Select" min="2020" max="2021" placeholder="Forward Date" (ionChange)="selectDate($event)"></ion-datetime>
我的 .ts 文件
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)))
}
这里的 nextDate 是预定义的日期,检查变量包含从日期选择器中选择的日期。
最佳答案
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);
关于javascript - 获取 ISO 字符串格式的 2 个日期相减后的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62529192/