How to take the last four months given given date

Asked

Viewed 701 times

-2

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

I want to take 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"
  • Ok, but the solutions given do not predict this problem and if you pass a date when the day is 31 you will get a wrong result. So make sure you don’t wear any days over 28. just test with 'Tue Oct 31 2017 14:42:35 GMT-0300' q vc see q, although the month given to be October, it returns the month preceding as October again.

3 answers

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 in the next/future 4 months just change i * -1 for i * 1

  • Thanks my dear, that’s just what I needed!

3

From what I understand you’re wanting to display the exact date(s) (s) every X months ago, see if it suits 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>

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>

Exit:

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
  • If play April 30 in this code it returns March 30, March 2, Feb 2 and Jan 2.

  • As only the month is relevant, to correct just put: d.setDate(1); before d.setMonth( d.getMonth() - 1 );

  • @Ccastro, huahuahua, with October 31 nothing returns as desired

  • @Ccastro Reply Edited

Browser other questions tagged

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