1 | import { differenceInCalendarDays } from "./differenceInCalendarDays.js";
|
2 | import { startOfYear } from "./startOfYear.js";
|
3 | import { toDate } from "./toDate.js";
|
4 |
|
5 | /**
|
6 | * The {@link getDayOfYear} function options.
|
7 | */
|
8 |
|
9 | /**
|
10 | * @name getDayOfYear
|
11 | * @category Day Helpers
|
12 | * @summary Get the day of the year of the given date.
|
13 | *
|
14 | * @description
|
15 | * Get the day of the year of the given date.
|
16 | *
|
17 | * @param date - The given date
|
18 | * @param options - The options
|
19 | *
|
20 | * @returns The day of year
|
21 | *
|
22 | * @example
|
23 | * // Which day of the year is 2 July 2014?
|
24 | * const result = getDayOfYear(new Date(2014, 6, 2))
|
25 | * //=> 183
|
26 | */
|
27 | export function getDayOfYear(date, options) {
|
28 | const _date = toDate(date, options?.in);
|
29 | const diff = differenceInCalendarDays(_date, startOfYear(_date));
|
30 | const dayOfYear = diff + 1;
|
31 | return dayOfYear;
|
32 | }
|
33 |
|
34 | // Fallback for modularized imports:
|
35 | export default getDayOfYear;
|