I'm trying to compare a "Date" type of data with an "Any" type of data, but the comparison is not working.
I'm getting the date in this code:
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() 1).padStart(2, '0');
var yyyy = (today.getFullYear());
today = dd '/' mm '/' yyyy;
And I'm doing the comparison here:
for (const event of events) {
if (event.fecha_fin >= today && event.hora_fin > my_time) {
console.log(event.fecha_fin > today)
eventsWithLowestPrice.push(event.id_evento_fk)
console.log(event.id_evento_fk)
}
}
The data type of event.fecha_fin is "Any".
I've tried to change both fecha_fin and today into "Number" data type but I don't really know how to do it.
I've tried changing the data type of "today" and "fecha_fin" to "Number" but I don't really know how to do it.
CodePudding user response:
Parse the strings as dates with
function stringToDate(str) {
const [dd, mm, yyyy] = str.split('/');
return new Date(yyyy, mm - 1, dd);
}
and compare the dates:
for (const event of events) {
const date1 = stringToDate(event.fecha_fin);
const date2 = stringToDate(today);
if (date1 >= date2 && event.hora_fin > my_time) {
console.log(date1 > date2)
eventsWithLowestPrice.push(event.id_evento_fk)
console.log(event.id_evento_fk)
}
}
Example:
function stringToDate(str) {
const [dd, mm, yyyy] = str.split('/');
return new Date(yyyy, mm - 1, dd);
}
const events = [{fecha_fin: '01/01/2020', hora_fin: 2}];
const today = '02/02/2002';
const my_time = 1;
for (const event of events) {
const date1 = stringToDate(event.fecha_fin);
const date2 = stringToDate(today);
if (date1 >= date2 && event.hora_fin > my_time) {
console.log(date1 > date2)
//eventsWithLowestPrice.push(event.id_evento_fk)
console.log(event.id_evento_fk)
}
}