Which operator should I use in Javascript c:if when I want to be between two variables?

Asked

Viewed 46 times

0

Gentlemen, I need to know which operator to use in c:if of Javascript when I want to make a decision between themselves, for example, I need a background of a Div turned red when it is between the date x and y, I would like to know which is the operator "BETWEEN"follows an excerpt from my code.

<c:if test ="${realizadoVN dfim <> dtini && ${realizadoVN <metaVN}">
	<div class="container">
		<div class="marca" ><h1>VN</h1></div> 
		<div class="meta"style="font-size: 25px;">META: <fmt:formatNumber value = "${VN.rows[0].META}" type="currency"/></div>
		<div class="rel" style= "background-color:red; color:white; font-size:30px;">ACUMULADO <br><br><fmt:formatNumber value = "${realizadoVN}" type="currency"/><br><h1> <c:out value="${VN.rows[0].ACUMULADO}"/>%</h1></br></div>
	</div>
</c:if>

  • I don’t know what language you’re using, but it wouldn’t be dini <= realizado && realizado <= dfim?

  • Kadu, and the goal that would be the second condition would enter where in this case? because I have two conditions in the same decision loop. <c:if test ="${realizadoVN dfim <> dtini && ${realizadoVN <metaVN}">

  • How is everything and &&, Just put it later without any problems. Just so I understand, SVN is your date?

  • The date is being represented by the variable DTINI and DTFIN ( Start Date and End Date) the realized VN is the variable that receives the value of my billing. What I would actually like is a function within Javascript where I could know the last day of the month and with that subtract minus 7, if you know would be welcome, because this solution that I am trying to apply at this moment I realized directly by SQL and I am trying to bring to my application, so the initial question.

1 answer

1

There is an excellent Javascript library called Momentjs to work with dates, follow an example:

var input = document.getElementById('dataInput');
var div = document.getElementById('minhaDiv');
  
var dini = moment().endOf('month').add(-7, 'd');
var dfim = moment().endOf('month');

document.getElementById('certoSpan').innerHTML = dini.format('DD/MM/YYYY')
document.getElementById('cuidadoSpan').innerHTML = dini.format('DD/MM/YYYY') + ' ~ ' + dfim.format('DD/MM/YYYY')
document.getElementById('ruimSpan').innerHTML = dfim.format('DD/MM/YYYY')

input.addEventListener('change', function() {
  var data = moment(input.value);
  

  
  if (data.isBefore(dini, 'd')) {
    div.style.backgroundColor = 'green';
    div.innerHTML = 'Tudo certo! :)';
  } else if (data.isBetween(dini, dfim, 'd', '[]')) {
    div.style.backgroundColor = 'yellow';
    div.innerHTML = 'Cuidado! :|';
  } else if (data.isAfter(dfim, 'd')) {
    div.style.backgroundColor = 'red';
    div.innerHTML = 'Deu ruim! :(';
  }
  
  

});
#minhaDiv {
  padding: 10px;
  margin: 10px;
  border: 2px solid #000;
  text-align: center;
  color: #000;
  font-weight: bold;
}
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<div id="minhaDiv">MINHA DIV</div>
<input type="date" id="dataInput">
<p>Tudo certo até <span id="certoSpan"></span></p>
<p>Cuidado entre: <span id="cuidadoSpan"></span></p>
<p>Deu ruim depois de: <span id="ruimSpan"></span></p>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.