UNPKG

1.01 kBJavaScriptView Raw
1"use strict";
2exports.setQuarter = setQuarter;
3var _index = require("./setMonth.js");
4var _index2 = require("./toDate.js");
5
6/**
7 * @name setQuarter
8 * @category Quarter Helpers
9 * @summary Set the year quarter to the given date.
10 *
11 * @description
12 * Set the year quarter to the given date.
13 *
14 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
15 *
16 * @param date - The date to be changed
17 * @param quarter - The quarter of the new date
18 *
19 * @returns The new date with the quarter set
20 *
21 * @example
22 * // Set the 2nd quarter to 2 July 2014:
23 * const result = setQuarter(new Date(2014, 6, 2), 2)
24 * //=> Wed Apr 02 2014 00:00:00
25 */
26function setQuarter(date, quarter) {
27 const _date = (0, _index2.toDate)(date);
28 const oldQuarter = Math.trunc(_date.getMonth() / 3) + 1;
29 const diff = quarter - oldQuarter;
30 return (0, _index.setMonth)(_date, _date.getMonth() + diff * 3);
31}