I am facing an issue with comparing dates in my ion-datetime
input and a date variable obtained from the server. The problem arises because when I retrieve the value from the input, it includes the time field along with the date. As a result, using the getTime()
method for comparison does not produce accurate results. Do you have any suggestions on how to transform the date or reset the time before making the comparison?
Here is the code snippet of my current method:
ngOnInit() {
this.desde = new Date(this.navParams.get('desde'));
this.hasta = new Date(this.navParams.get('hasta'));
this.auth.userData$.subscribe((res: any) => {
this.authUser = res;
this.postData = {token: this.authUser.token};
if (this.postData) {
this.planificadorService.planificadorData(this.postData).subscribe((res2: any) => {
this.planificadorData = res2.planificadorData;
this.planificadorData.forEach(item => {
if (item.fecha !== '') {
const fechaPlan = new Date(item.fecha);
if (fechaPlan.getTime() >= this.desde.getTime() && fechaPlan.getTime() <= this.hasta.getTime()) {
this.visblePLanificadorData.push(item);
}
}
});
console.log(this.visblePLanificadorData);
});
}
});
}
Input Dates:
desde: "2020-01-24T18:23:18.633-05:00"
hasta: "2020-01-27T18:23:18.634-05:00"
Expected Format:
desde: "2020-01-24T00:00:00.000-00:00"
hasta: "2020-01-27T00:00:00.000-00:00"