Java Script Date Comparison Help

Asked

Viewed 48 times

0

I need to compare final date of a service contract to put in a report that will have 4 types of status:

1- contrato em vigor (Verde)
2- contrato próximo ao vencimento (Amarelo)
3- contrato vencido (Vermelho)
4- contrato com prazo indeterminado (Azul)

I’m receiving 2 values by the user, dt_inicio and dt_final, need that the dt_final is compared with the (new Date) dt_atual that is to say, "If the dt_final is >= dt_current then the contract is overdue." | " If the dt_final is < dt_current then I need the result of that date to be transformed into months to be able to compare, example dt_final 27/10/2018 < dt_current 27/06/2018 = 4 months that will add this value in the dt_vigor variable.

Se dt_vigor  =< 4(meses) então adiciona valor em (próximo_vencimento);
se dt_vigor > 4(meses) então adiciona valor em (vencimento_ok)
se não (sem_vencimento);

1 answer

0

Working with javascript dates is easier using Moment.js.

The moment it’s okay documented and has several examples of use.

Below is an example of the comparison of the current date with another(dt_vigor);

var dt_vigor = moment("01/05/2018", "DD/MM/YYYY"); 
if(dt_vigor <= moment().subtract(4, 'month')){ 
   alert('dt_vigor tem 04 meses ou mais.'); 
   //adiciona valor em (próximo_vencimento); 
   }else{ 
   alert('dt_vigor tem menos de 04 meses.'); 
   //adiciona valor em (vencimento_ok) 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

I hope I’ve helped!

Browser other questions tagged

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