Lightning returns the date as a normal string, so you need to convert it to a date via new Date(attributeValue)
. Here's how that might look like:
({
validate: function(component, event, helper) {
var start = component.get("v.startDate"),
end = component.get("v.endDate"),
yearDiff, monthDiff, dayDiff, withinTenYears;
if(start && end) {
start = new Date(start);
end = new Date(end);
yearDiff = end.getFullYear() - start.getFullYear();
monthDiff = end.getMonth() - start.getMonth();
dayDiff = end.getDate() - start.getDate();
withinTenYears =
// difference of less than 10 numerical years (e.g. 2018-2027)
yearDiff < 10 ||
// ten numerical years, but end month less than start month
(yearDiff == 10 && monthDiff < 0) ||
// ten numerical years, same month, less or equal to the day
(yearDiff == 10 && monthDiff == 0 && dayDiff <= 0);
if(!withinTenYears) {
// do whatever you like here
}
}
}
})
Feel free to adjust as necessary.