Created
June 27, 2013 12:42
-
-
Save kevinsimper/5876101 to your computer and use it in GitHub Desktop.
Get the lastest 4 quaters in javascript.
You could push the result into an array to use it in your script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var currentQuater = parseInt(new Date().getMonth() / 3 + 1, 0); | |
// Quater has to finished | |
var firstQuater = currentQuater - 1; | |
// Get current year to know when we get before January. | |
var firstYear = new Date().getFullYear(); | |
for (var i = 0; i < 4; i++) { | |
// If quater is equal to zero, go back a year | |
if (firstQuater === 0) { | |
firstQuater = 4; | |
firstYear--; | |
} | |
// Get the month | |
// First quater starts from januar which is month 0 | |
var firstMonth = (firstQuater - 1) * 3; | |
// Plus 2 month to get the first month | |
// Add 1 again to use the trick to get the lastest day | |
var lastMonth = (firstQuater - 1) * 3 + 2 + 1; | |
// If you put 0 as the day you get the last day in the previous month. | |
console.log(firstQuater, firstYear, new Date(firstYear, firstMonth, 1), new Date(firstYear, lastMonth, 0)); | |
// Count down quater | |
firstQuater--; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment