JavaScript jquery find previous 12 months date from given date jquery find previous sibling,jquery find previous tr,jquery find

Here , In jquery I am trying to find previous 12 month date from given date. but, the previous 12 months dates are wrong.

>     var now = new Date();
>     var months = new Array( "January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November",
> "December");
>     for(var i=0; i<=11;i++){
>     var past = now.setMonth(now.getMonth() -i);
>     console.log(months[now.getMonth()]+' '+now.getFullYear());
>     }

I am getting below output.

June 2016
May 2016
March 2016
December 2015
August 2015
March 2015
September 2014
February 2014
June 2013
September 2012
November 2011
December 2010
Answer:1

All you have to do is change:

now.getMonth() - i

To this

now.getMonth() - 1

That's it. Because you're modifying now in each iteration of your loop, just subtract one month instead of the loop variable.

See working example here: http://jsbin.com/nibizaqayu/edit?js,console


Also note that your past variable is doing nothing at this point, so your loop can look like this:

for(var i=0; i<=11;i++){
   now.setMonth(now.getMonth() - 1);
   console.log(months[now.getMonth()]+' '+now.getFullYear());
}

Another option would be to create a new Date instance as the other answers have shown, and it works equally well. However I personally find this to be simpler.

Answer:2

I'm trying to set up automatic testing, using Mocha and PhantomJS on Selenium with Node. I'm using selenium-webdriver library since it seems to be a popular one, but when I run the test and try to ...

I'm trying to set up automatic testing, using Mocha and PhantomJS on Selenium with Node. I'm using selenium-webdriver library since it seems to be a popular one, but when I run the test and try to ...

  1. error done() invoked with non-error

Ok i have noticed this bug with FireFox and IE 11 how to replicate it: Start scrolling on the fixed column While its scrolling move the mouse over to the other data Result: not aligned rows Live ...

Ok i have noticed this bug with FireFox and IE 11 how to replicate it: Start scrolling on the fixed column While its scrolling move the mouse over to the other data Result: not aligned rows Live ...

  1. jquery datatables vertical scroll
  2. jquery datatables vertical align
  3. jquery datatables vertical header
  4. jquery datatables vertical
  5. jquery datatable vertical scroll not working
  6. jquery datatable vertical scroll header width
  7. jquery datatable vertical align middle
  8. jquery datatable vertical align top
  9. jquery datatable vertical border
  10. jquery datatable remove vertical scrollbar
  11. jquery datatable column vertical align
  12. jquery datatable add vertical scroll
  13. jquery datatable disable vertical scroll
  14. jquery datatable hide vertical scrollbar
  15. jquery datatables fixed header vertical scroll
  16. jquery datatables header misaligned with vertical scrolling
  17. jquery datatable vertical height

I have an option set field in Dynamics CRM that has two values: "In Progress" (default) and "Completed". Using JavaScript, I want to issue a confirm box that triggers during the field event OnChange. ...

I have an option set field in Dynamics CRM that has two values: "In Progress" (default) and "Completed". Using JavaScript, I want to issue a confirm box that triggers during the field event OnChange. ...

  1. dynamics 2016 javascript

I have a page with two textareas and two buttons that copy the content of each textarea respectively to the user's clipboard when pressed. When the button is pressed and the copying is successful, a ...

I have a page with two textareas and two buttons that copy the content of each textarea respectively to the user's clipboard when pressed. When the button is pressed and the copying is successful, a ...