How to pick up the last four months on a given date

-2

Good afternoon everyone! I need a function that I enter a date and it returns me the date of the last four months, can be done in angular js or javascript, can you give me some help with this?

I want to get the last four months / year of a date for example

"Fri Sep 22 2017 14:42:35 GMT-0300" 

and the output example

"Fri Sep 22 2017 14:42:35 GMT-0300", 
"Fri Out 22 2017 14:42:35 GMT-0300", 
"Fri Nov 22 2017 14:42:35 GMT-0300", 
"Fri Dez 22 2017 14:42:35 GMT-0300"
    
asked by anonymous 22.09.2017 / 21:45

3 answers

3

Suggestion JavaScript :

function foobar( dt )
{
    var array = [];

    var d = new Date( dt );

    for( var i = 0; i < 4; i++ )
    {
        var m = d.getMonth();

        d.setMonth( m - 1 );

        if( d.getMonth() != m - 1 && (d.getMonth() != 11 || (m == 11 && d.getDate() == 1)))
            d.setDate(0);

        array[i] = new Date(d);
    }

    return array;
}

Testing with HTML :

<html>
    <head>
        <script>

            function foobar( dt )
            {
                var array = [];

                var d = new Date( dt );

                for( var i = 0; i < 4; i++ )
                {
                    var m = d.getMonth();

                    d.setMonth( m - 1 );

                    if( d.getMonth() != m - 1 && (d.getMonth() != 11 || (m == 11 && d.getDate() == 1)))
                        d.setDate(0);

                    array[i] = new Date(d);
                }

                return array;
            }

        </script>
    </head>

    <body>
        <script>

            var d = foobar( 'Sep 22 2017' );

            for(i = 0; i < 4; i++ )
                window.document.write( d[i] + '<BR>' );

            window.document.write( '<BR>' );

            var d = foobar( 'Apr 30 2017' );

            for(i = 0; i < 4; i++ )
                window.document.write( d[i] + '<BR>' );

            window.document.write( '<BR>' );

            var d = foobar( 'Oct 31 2017' );

            for(i = 0; i < 4; i++ )
                window.document.write( d[i] + '<BR>' );


        </script>
    </body>

</html>

Output:

Tue Aug 22 2017 00:00:00 GMT-0300
Sat Jul 22 2017 00:00:00 GMT-0300
Thu Jun 22 2017 00:00:00 GMT-0300
Mon May 22 2017 00:00:00 GMT-0300

Thu Mar 30 2017 00:00:00 GMT-0300
Tue Feb 28 2017 00:00:00 GMT-0300
Sat Jan 28 2017 00:00:00 GMT-0200
Wed Dec 28 2016 00:00:00 GMT-0200

Sat Sep 30 2017 00:00:00 GMT-0300
Wed Aug 30 2017 00:00:00 GMT-0300
Sun Jul 30 2017 00:00:00 GMT-0300
Fri Jun 30 2017 00:00:00 GMT-0300
    
22.09.2017 / 22:15
4

To generate the date of the day in the last four months:

function mesAnterior(date, diff) {
  const d = new Date(date);
  d.setMonth(d.getMonth() + diff);
  return d;
}

function dataNosUltimosQuatroMeses(data) {
  const datas = [];
  for (let i = 1; i < 5; i++) {
    datas.push(mesAnterior(data, i * -1));
  }
  return datas;
}

const hoje = new Date();
console.log(dataNosUltimosQuatroMeses(hoje));

If you want the next / future 4 months, just change i * -1 to i * 1

    
22.09.2017 / 22:13
3

I understand that you want to display the exact date (s) every X months ago, see if it caters to you:

function exibeData(){
  var dataPreenchida = document.getElementById("Data").value;
  var qtd = document.getElementById("Quantidade").value;
  if(dataPreenchida){
    while(qtd>0){
      console.log(subtraiMes(dataPreenchida, qtd));
      qtd--;
    }
  }
}

function subtraiMes(str, qtd){
    var d = new Date(str);
    d.setMonth( d.getMonth( ) - qtd );
    return d;
}
<div>
  <p>Preencha a data</p>
  <input type="date" name="Data" id="Data" />
</div>
<div>
  <p>Quantidade de meses atrás</p>
  <select name="Quantidade" id="Quantidade">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4" selected="selected">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
  </select>
</div>
<div>
  <button type="button" onclick="exibeData();">
    Exibe data
  </button>
</div>
    
22.09.2017 / 22:01