Help with date comparison in Java Script [duplicate]

0

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

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

I'm getting 2 values for the user, dt_inicio and dt_final , I need the dt_final to be compared with (new Date) dt_atual "If dt_final is> dt_atual then the contract is expired." | "If the dt_final is

asked by anonymous 27.06.2018 / 17:19

1 answer

0

Working with dates in javascript is easier using moment.js .

moment is well documented and has several usage examples.

Here is an example of comparing 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 have helped!

    
29.06.2018 / 21:19