Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current quarter in year with javascript

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 count the number of days left in the quarter?

like image 330
John Doe Avatar asked Aug 16 '12 05:08

John Doe


People also ask

How do you get the current quarter in typescript?

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.

How can I get current year in JavaScript?

JavaScript Date getFullYear() getFullYear() returns the full year (4 digits) of a date.

How do you get quarters in a month?

To get quarter from a date, you simply need to divide the month by 3 and round up the result to the nearest integer. Since each quarter of the year consists of 3 months, dividing the month of a date by 3 returns the number of 3-month intervals.


2 Answers

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.

like image 180
paxdiablo Avatar answered Sep 23 '22 02:09

paxdiablo


Given that you haven't provided any criteria for how to determine what quarter "*we are currently in", an algorithm can be suggested that you must then adapt to whatever criteria you need. e.g.

// For the US Government fiscal year // Oct-Dec = 1 // Jan-Mar = 2 // Apr-Jun = 3 // Jul-Sep = 4 function getQuarter(d) {   d = d || new Date();   var m = Math.floor(d.getMonth()/3) + 2;   return m > 4? m - 4 : m; } 

As a runnable snippet and including the year:

function getQuarter(d) {    d = d || new Date();    var m = Math.floor(d.getMonth() / 3) + 2;    m -= m > 4 ? 4 : 0;    var y = d.getFullYear() + (m == 1? 1 : 0);    return [y,m];  }    console.log(`The current US fiscal quarter is ${getQuarter().join('Q')}`);  console.log(`1 July 2018 is ${getQuarter(new Date(2018,6,1)).join('Q')}`);

You can then adapt that to the various financial or calendar quarters as appropriate. You can also do:

function getQuarter(d) {   d = d || new Date(); // If no date supplied, use today   var q = [4,1,2,3];   return q[Math.floor(d.getMonth() / 3)]; } 

Then use different q arrays depending on the definition of quarter required.

Edit

The following gets the days remaining in a quarter if they start on 1 Jan, Apr, Jul and Oct, It's tested in various browsers, including IE 6 (though since it uses basic ECMAScript it should work everywhere):

function daysLeftInQuarter(d) {   d = d || new Date();   var qEnd = new Date(d);   qEnd.setMonth(qEnd.getMonth() + 3 - qEnd.getMonth() % 3, 0);   return Math.floor((qEnd - d) / 8.64e7); } 
like image 44
RobG Avatar answered Sep 21 '22 02:09

RobG