Quarter of year

OSCC · JavaScript, Date · Oct 22, 2020

Returns the quarter and year to which the supplied date belongs to.

  • Use Date.prototype.getMonth() to get the current month in the range (0, 11), add 1 to map it to the range (1, 12).
  • Use Math.ceil() and divide the month by 3 to get the current quarter.
  • Use Date.prototype.getFullYear() to get the year from the given date.
  • Omit the argument, date, to use the current date by default.
const quarterOfYear = (date = new Date()) => [
  Math.ceil((date.getMonth() + 1) / 3),
  date.getFullYear()
];
quarterOfYear(new Date('07/10/2018')); // [ 3, 2018 ]
quarterOfYear(); // [ 4, 2020 ]

Recommended snippets

  • Day of year

    JavaScript, Date

    Gets the day of the year (number in the range 1-366) from a Date object.

  • Week of year

    JavaScript, Date

    Returns the zero-indexed week of the year that a date corresponds to.

  • Number of days in month

    JavaScript, Date

    Gets the number of days in the given month of the specified year.