获取减去 2 个 ISO 字符串格式的日期后的天数
Get number of days after subtracting 2 dates which is in ISO String format
我想计算两个日期(两个日期均为 isoString 格式)之间相差后的天数。当我尝试减去它 returns 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);
您尝试计算两个字符串。这就是你得到 NaN 的原因。
您应该将这两个日期转换为整数。例如:
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)))
}
在此示例中,您将动态的日期字符串转换为可以在其上使用 getTime() 的日期对象。这样,您就可以得到 1.1.1970 和日期之间的延迟(以毫秒为单位)。
我想计算两个日期(两个日期均为 isoString 格式)之间相差后的天数。当我尝试减去它 returns 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);
您尝试计算两个字符串。这就是你得到 NaN 的原因。 您应该将这两个日期转换为整数。例如:
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)))
}
在此示例中,您将动态的日期字符串转换为可以在其上使用 getTime() 的日期对象。这样,您就可以得到 1.1.1970 和日期之间的延迟(以毫秒为单位)。