javascript - How do you loop backwards through the months starting with the current one? -
I am using javascript, am trying to fill a barchart. The last line on the chart is the current month (November 2014 at the time of writing, or 10 if you use the date of javascript). GetMonth () method). It starts counting when using getMonth () in the next month (December 2013, or 11) of last year
My question: 12 months through the time starting from 11 months before the present time What is the most effective way of loop?
My favorite output will be:
- December 2014
- February 2014
- ...
- November 2014
You can use for loop and by the current date Decrease months. As shown below:
var dateObj = New Date (); Var dateStrings = []; Var dateFormatOptions = {Month: 'Long', Year: 'Numerical'}; {DateStrings.unshift (dateObj.toLocaleString ('en-US', dateFormatOptions) for (Var i = 0; i & lt; 12; ++ i); DateObj.setMonth (dateObj.getMonth () - 1); } Document.getElementById ("Output"). InnerHTML = dateStrings.join ("\ n"); & lt; Prior id = "output" & gt; & Lt; / Pre & gt; This code will want to create an array by date strings by you.
Comments
Post a Comment