Get current quarter in year with javascript

Viewed 79384

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?

13 Answers
function getQuarter(d) {
  return Math.floor(d.getMonth()/3) + 1);
}

EDIT: i left out %4, totally right, thanks Clement

You can use moment package:

Answer of your question using moment package is:

moment().quarter()

Below are the start and end dates of a quarter using the moment package:

START DATE OF QUARTER

moment().quarter(moment().quarter()).startOf('quarter');

Would return the current quarter with the date set to the quarter starting date.

moment("2019", "YYYY").quarter(4).startOf('quarter');

Would return the starting date of the 4th quarter of the year "2019".

moment().startOf('quarter');

Would return the starting date of the current quarter of current year.

END DATE OF QUARTER

moment().quarter(moment().quarter()).endOf('quarter');

Would return the current quarter with the date set to quarter ending date.

moment("2019", "YYYY").quarter(4).endOf('quarter');

Would return the ending date of the 4th quarter of the year "2019".

moment().endOf('quarter');

Would return the ending date of the current quarter of current year.

This worked for me!

var d = new Date();
var quarter = Math.ceil(d.getMonth() / 3);

console.log(quarter)

// 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();
  }

In my case, quarter depends on the input quarter start date (month, day) and quarter duration (3, 6)

function getCurrentQuarter(startDateString, quarterDuration) {
    var startDate = new Date(startDateString)
    // default order of months
    var months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    let startMonth = startDate.getMonth() + 1
    let startMonthIndex = months.indexOf(startMonth)
    // rotate months to get start month first
    if (startMonthIndex != 0) {
        var monthsNew = months.slice(startMonthIndex, months.length)
        months = monthsNew.concat(months.slice(0, startMonthIndex))
    }
    
    let today = new Date()
    let thisMonth = today.getMonth() + 1
    let thisMonthIndex = months.indexOf(thisMonth) + 1
    // compare with date
    let thisDay = today.getDate()
    let startDay = startDate.getDate()
    if (thisDay < startDay) {
        // this is previous month
        if (thisMonthIndex == 1) {
            thisMonthIndex = 12
        } else {
            thisMonthIndex = thisMonthIndex - 1
        }
    }
    // find quarter
    let thisQuarter = Math.ceil(thisMonthIndex / quarterDuration)

    return thisQuarter
}

getCurrentQuarter('2021-03-15', 3)

Universal, fast performance (no division, no floats), easy modified for other standard

const now = new Date()
const currentMonth = now.getMonth() + 1
const currentQuarter = {
   1: 1, // January
   2: 1, // February
   3: 1, // March

   4: 2, // April
   5: 2, // May
   6: 2, // June

   7: 3, // July
   8: 3, // August
   9: 3, // September

   10: 4, // October
   11: 4, // November
   12: 4, // December
}[currentMonth]

console.log(currentQuarter)

I don't know why you all use floor, when you can simply use ceil.

function getQuarter(m) {
  return Math.ceil(m / 3)
}

// Test it
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map(getQuarter);

If month counts from 0, just add 1 to month number:

function getQuarter(date) {
  const m = date.getMonth() + 1;
  return Math.ceil(m / 3)
}

Here is my take

var todayDate = new Date(new Date().setUTCHours(0, 0, 0, 0));
var currentQuarter = Math.floor(todayDate.getMonth() / 3);
var firstDate = new Date(
  new Date(todayDate.getFullYear(), currentQuarter * 3, 1).setUTCHours(0, 0, 0, 0)
);
var endDate = new Date(
  new Date(
    firstDate.getFullYear(),
    firstDate.getMonth() + 3,
    0
  ).setUTCHours(0, 0, 0, 0)
);
console.log(endDate)

Related