lightweight
lightweight

Reputation: 3327

breaking dates out by the first day of each quarter

I have a method right now that takes a date in string format like YYYY-MM and breaks it out to a quarter format like Q1 2016.

var mth_dt = new Date(d.mth_dt).getMonth() + 1;
var quarter = Math.ceil(mth_dt / 3)
var yrq = d.mth_dt.substr(0,4).toString();
var qtrYr = "Q"+quarter+" "+yrq

//console.log("qtrYr: ", qtrYr);

return qtrYr;

what I was wondering now, is how do I get it to result in the 1st date of each quarter? For instance, Q1 2016 would be 1/1/2016 and Q2 2016 would be 4/1/2016.

Upvotes: 2

Views: 81

Answers (1)

Oliver
Oliver

Reputation: 208

If the month and day for a quarter are the same each year, why not do something like this:

GetQuarter = function(month,year){
var quarter = "";

if (month/3 <= 1) 
	quarter = "Q1 01/01/"; 
else if(month/3 <= 2) 
	quarter ="Q2 04/01/"; 
else if(month/3 <= 3) 
	quarter ="Q3 07/01/";
else 
	quarter = "Q4 10/01/";

return quarter += year;
}

document.write(GetQuarter(11,2016)) // Q4 10/01/2016

Upvotes: 2

Related Questions