Reputation: 1029
This is Duplicate of this question. I don't know how to compare the diff. For example if the diff equal to greater than 14 days then the text box should be red. if 7 to 13 days it would be yellow and below 6 days it would be green.
code:
function compare() {
var firstDate = moment($("#date1").val(), "MM-DD-YYYY");
var secondDate = moment($("#date2").val(), "MM-DD-YYYY");
console.log(firstDate.inspect(), secondDate.inspect());
if (firstDate.isValid() && secondDate.isValid()) {
// you need a validation before using diff function of momentjs
var diff = Math.abs(firstDate.diff(secondDate, 'days'));
console.log(diff);
// you also need to remove all classes before adding new class
$("#status").removeClass("redBg").removeClass("greenBg").removeClass("yellowBg");
if (diff => 14) {
$("#status").addClass('redBg');
} else if (7 < diff =< 9) {
$("#status").addClass('greenBg');
} else if(diff >= 6) {
$("#status").addClass('yellowBg');
}
} else {
$("#status").addClass('yellowBg');
}
});
.greenBg {
background: green;
}
.yellowBg {
background: yellow;
}
.redBg {
background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<html>
<body>
<input type="text" id="date1" onchange=/>
<input type="text" id="date2" onchange='compare()'/>
<input readonly type="text" id="status"/>
</body>
</html>
Upvotes: 0
Views: 87
Reputation: 136
This is not a valid condition: else if(7 < diff =< 9)
, it shoud be else if(diff > 7 && diff <= 9)
Upvotes: 1