How can I get the current quarter we are in with javascript? I am trying to detect what quarter we are currently in, e.g. 2.
EDIT And how can I coun
if the first solution doesn't work than you can just adjust it to the range you would like
var today = new Date();
var month = now.getMonth();
var quarter;
if (month < 4)
quarter = 1;
else if (month < 7)
quarter = 2;
else if (month < 10)
quarter = 3;
else if (month < 13)
quarter = 4;
Assuming January through March are considered Q1 (some countries/companies separate their financial year from their calendar year), the following code should work:
var today = new Date();
var quarter = Math.floor((today.getMonth() + 3) / 3);
This gives you:
Month getMonth() quarter
--------- ---------- -------
January 0 1
February 1 1
March 2 1
April 3 2
May 4 2
June 5 2
July 6 3
August 7 3
September 8 3
October 9 4
November 10 4
December 11 4
As to how to get the days remaining in the quarter, it's basically figuring out the first day of the next quarter and working out the difference, something like:
var today = new Date();
var quarter = Math.floor((today.getMonth() + 3) / 3);
var nextq;
if (quarter == 4) {
nextq = new Date (today.getFullYear() + 1, 1, 1);
} else {
nextq = new Date (today.getFullYear(), quarter * 3, 1);
}
var millis1 = today.getTime();
var millis2 = nextq.getTime();
var daydiff = (millis2 - millis1) / 1000 / 60 / 60 / 24;
That's untested but the theory is sound. Basically create a date corresponding to the next quarter, convert it and today into milliseconds since the start of the epoch, then the difference is the number of milliseconds.
Divide that by the number of milliseconds in a day and you have the difference in days.
That gives you (at least roughly) number of days left in the quarter. You may need to fine-tune it to ensure all times are set to the same value (00:00:00) so that the difference is in exact days.
It may also be off by one, depending on your actual definition of "days left in the quarter".
But it should be a good starting point.
function getQuarter(d) {
return Math.floor((d.getMonth()/3 % 4) + 1);
}
Depend on month
var date = new Date();
var quarter = parseInt(date.getMonth() / 3 ) + 1 ;
Depend on Date
var date = new Date();
var firstday = new Date(date.getFullYear(),0,1); // XXXX/01/01
var diff = Math.ceil((date - firstday) / 86400000);
// a quarter is about 365/4
quarter = parseInt( diff / ( 365/ 4 )) + 1
// if today is 2012/01/01, the value of quarter is 1.
// Set Period Function
SetPeriod(SelectedVal) {
try {
if (SelectedVal === '0') { return; }
if (SelectedVal != null) {
let yrf: number, mtf: number, dyf: number, yrt: number, mtt: number, dyt: number, dtf: any, dtt: any;
let dat = new Date();
let q = 0;
switch (SelectedVal) {
case '-1': // Not specify
frm = ''; to = '';
return;
case '0': // As specify
break;
case '1': // This Month
yrf = yrt = dat.getUTCFullYear();
mtf = mtt = dat.getUTCMonth();
dyf = 1; dyt = this.getDaysInMonth(mtf, yrf);
break;
case '2': // Last Month
dat.setDate(0); // 0 will result in the last day of the previous month
dat.setDate(1); // 1 will result in the first day of the month
yrf = yrt = dat.getUTCFullYear();
mtf = mtt = dat.getUTCMonth();
dyf = 1; dyt = this.getDaysInMonth(mtf, yrf);
break;
case '3': // This quater
q = Math.ceil((dat.getUTCMonth()) / 3);
// tslint:disable-next-line:no-switch-case-fall-through
case '4': // Last quater
if (q === 0) { q = Math.ceil(dat.getUTCMonth() / 3) - 1; if (q === 0) { q = 1; } }
yrf = yrt = dat.getUTCFullYear();
if (q === 1) {
mtf = 0; mtt = 2;
dyf = 1; dyt = 31;
} else if (q === 2) {
mtf = 3; mtt = 5;
dyf = 1; dyt = 30;
} else if (q === 3) {
mtf = 6; mtt = 8;
dyf = 1; dyt = 30;
} else if (q === 4) {
mtf = 9; mtt = 11;
dyf = 1; dyt = 31;
}
break;
case '6': // Last year
dat = new Date(dat.getUTCFullYear(), 0, 1);
// tslint:disable-next-line:no-switch-case-fall-through
case '5': // This year
yrf = yrt = dat.getUTCFullYear();
mtf = 0; mtt = 11;
dyf = 1; dyt = 31;
break;
}
// Convert to new Date
dtf = new Date(yrf, mtf, dyf);
dtt = new Date(yrt, mtt, dyt);
console.log('dtf', dtf);
console.log('dtt', dtt);
}
} catch (e) {
alert(e);
}
}
// Get Day in Month
getDaysInMonth = (month: number, year: number) => {
return new Date(year, month + 1, 0).getDate();
}
It's not efficient or readable but it's in oneliner flavour.
(new Date(new Date().getFullYear(), Math.floor((new Date().getMonth() + 3) / 3) * 3, 1) - new Date()) / 86400000