Date.prototype.getMonth()
to get the current month in the range (0, 11), add 1
to map it to the range (1, 12).Math.ceil()
and divide the month by 3
to get the current quarter.Date.prototype.getFullYear()
to get the year from the given date
.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 ]
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️