Moment.js is driving me nuts.
I have dates in this format
10-Jul-2019 inside my array of objects alldata
and here is my code:
I need to filter out the objects where DueDate is outside of the range of last 90 days.
The issue is that the comparisons is not working properly...it's telling me that 11-05-2019 > 08-08-2019
Am I missing something or is there a bug in moment.js? Feel free to suggest any method that does not use moment.js
var todate = moment().format("DD-MM-YYYY");
var fromdate = moment().subtract(90, "days").format("DD-MM-YYYY");
var data = [];
for (i = 0; i < alldata.length; i++) {
duedate = moment(alldata[i].DueDate, "DD-MMM-YYYY").format('DD-MM-YYYY');
if ( duedate >= fromdate) {
alert("good!");
} else
alert("bad!");
}
You are not comparing integers or numbers format
returns a string.
Why not using the isAfter
built-in method from moment.js library which compares two "moments".
var fromdate = new moment().subtract(90, "days");
for (i = 0; i < alldata.length; i++) {
let checkDate = new moment(alldata[i].DueDate,"DD-MMM-YYYY")
let isAfter = checkDate.isAfter(fromdate);
if ( isAfter ) {
alert("good!");
} else
alert("bad!");
}