Lets say there is one age variable and another variable for the range of the age. I already programmed this script to calculate the dates for search between, but it doesn't always calculate the correct start/end date.
- If I use as age 30 and range 0, it works correctly (has the person with the age 30).
- If I use as age 31 and range 1, it works correctly (has the person with the age 30).
- If I use as age 30 and range 1, it works correctly (has the person with the age 30).
- If I use as age 30 and range 5, it works correctly (has the person with the age 30).
- If I use as age 29 and range 1, it doesn't work correctly (hasn't the person with the age 30).
So it seems there is an error with the end date I doesn't find? Here's my script:
if (ageRange && (ageRange < 0 || ageRange > 20)) {
ageRange = 10;
}
if (age && age >= 18 && age <= 100) {
var now = new Date();
var now2 = new Date();
var startDate = new Date(now.setFullYear(now.getFullYear() - (age - ageRange)));
startDate.setHours(23);
startDate.setMinutes(59);
startDate.setSeconds(59);
var endDate = new Date(now2.setFullYear(now2.getFullYear() - (age + ageRange)));
endDate.setHours(0);
endDate.setMinutes(0);
endDate.setSeconds(0);
var findBirthObject = { $lte: startDate, $gte: endDate };
if (ageRange == 0) {
startDate = new Date(startDate.valueOf() + 1000);
startDate.setFullYear(startDate.getFullYear() - 1);
findBirthObject = { $gte: startDate, $lte: endDate };
}
}