1 | import { setMonth } from "./setMonth.js";
|
2 | import { toDate } from "./toDate.js";
|
3 |
|
4 | /**
|
5 | * The {@link setQuarter} function options.
|
6 | */
|
7 |
|
8 | /**
|
9 | * @name setQuarter
|
10 | * @category Quarter Helpers
|
11 | * @summary Set the year quarter to the given date.
|
12 | *
|
13 | * @description
|
14 | * Set the year quarter to the given date.
|
15 | *
|
16 | * @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).
|
17 | * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
|
18 | *
|
19 | * @param date - The date to be changed
|
20 | * @param quarter - The quarter of the new date
|
21 | * @param options - The options
|
22 | *
|
23 | * @returns The new date with the quarter set
|
24 | *
|
25 | * @example
|
26 | * // Set the 2nd quarter to 2 July 2014:
|
27 | * const result = setQuarter(new Date(2014, 6, 2), 2)
|
28 | * //=> Wed Apr 02 2014 00:00:00
|
29 | */
|
30 | export function setQuarter(date, quarter, options) {
|
31 | const date_ = toDate(date, options?.in);
|
32 | const oldQuarter = Math.trunc(date_.getMonth() / 3) + 1;
|
33 | const diff = quarter - oldQuarter;
|
34 | return setMonth(date_, date_.getMonth() + diff * 3);
|
35 | }
|
36 |
|
37 | // Fallback for modularized imports:
|
38 | export default setQuarter;
|