Subtracting 30 days from a given date in JavaScript [duplicate]

1

How do I subtract 30 days from a certain date, assuming that in the DT_VENC field it reports the date of a due date and in the DT_AVISO field returns the date 30 days before the one informed, in the format dd/mm/aaaa ?

    
asked by anonymous 27.11.2017 / 23:54

2 answers

-3

I believe this is what you want:

function getDataAviso(dataVencimento, dias){
    var data = new Date(dataVencimento);

    data.setDate(data.getDate() - dias);

    var dia = data.getDate();
    if (dia.toString().length == 1)
      dia = "0"+dia;
    var mes = data.getMonth()+1;
    if (mes.toString().length == 1)
      mes = "0"+mes;
    var ano = data.getFullYear();
    return dia+"/"+mes+"/"+ano;
}  

The way to add or subtract date is by creating a Date object and using the setDate method to set a new value by adding or subtracting the amount of days you want.

    
28.11.2017 / 00:29
-3
 var DT_VENC = new Date();
 var DT_AVISO = new Date().setDate(DT_VENC.getDate() - 30).toLocaleString("pt-BR");'
    
28.11.2017 / 01:43