ts-time-utils 4.0.1 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -30
- package/dist/{age.js → age.cjs} +14 -6
- package/dist/{calculate.js → calculate.cjs} +30 -18
- package/dist/{calendar.js → calendar.cjs} +80 -39
- package/dist/{calendars.js → calendars.cjs} +48 -23
- package/dist/{chain.js → chain.cjs} +41 -40
- package/dist/{compare.js → compare.cjs} +58 -28
- package/dist/constants.cjs +19 -0
- package/dist/{countdown.js → countdown.cjs} +16 -7
- package/dist/{cron.js → cron.cjs} +20 -9
- package/dist/{dateRange.js → dateRange.cjs} +42 -26
- package/dist/{duration.js → duration.cjs} +56 -44
- package/dist/esm/chain.js +0 -5
- package/dist/esm/finance.d.ts +236 -0
- package/dist/esm/finance.d.ts.map +1 -0
- package/dist/esm/finance.js +495 -0
- package/dist/esm/healthcare.d.ts +260 -0
- package/dist/esm/healthcare.d.ts.map +1 -0
- package/dist/esm/healthcare.js +447 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -0
- package/dist/esm/naturalLanguage.d.ts +1 -3
- package/dist/esm/naturalLanguage.d.ts.map +1 -1
- package/dist/esm/naturalLanguage.js +9 -2
- package/dist/esm/plugins.d.ts +0 -6
- package/dist/esm/plugins.d.ts.map +1 -1
- package/dist/esm/plugins.js +36 -42
- package/dist/esm/recurrence.d.ts.map +1 -1
- package/dist/esm/recurrence.js +3 -5
- package/dist/esm/scheduling.d.ts +206 -0
- package/dist/esm/scheduling.d.ts.map +1 -0
- package/dist/esm/scheduling.js +329 -0
- package/dist/esm/timezone.d.ts +6 -1
- package/dist/esm/timezone.d.ts.map +1 -1
- package/dist/esm/timezone.js +106 -66
- package/dist/esm/types.d.ts +0 -4
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/finance.cjs +512 -0
- package/dist/finance.d.ts +236 -0
- package/dist/finance.d.ts.map +1 -0
- package/dist/{fiscal.js → fiscal.cjs} +36 -17
- package/dist/{format.js → format.cjs} +83 -70
- package/dist/healthcare.cjs +462 -0
- package/dist/healthcare.d.ts +260 -0
- package/dist/healthcare.d.ts.map +1 -0
- package/dist/{holidays.js → holidays.cjs} +52 -25
- package/dist/index.cjs +595 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/{interval.js → interval.cjs} +24 -11
- package/dist/{iterate.js → iterate.cjs} +84 -41
- package/dist/{locale.js → locale.cjs} +54 -26
- package/dist/{naturalLanguage.js → naturalLanguage.cjs} +36 -23
- package/dist/naturalLanguage.d.ts +1 -3
- package/dist/naturalLanguage.d.ts.map +1 -1
- package/dist/{parse.js → parse.cjs} +24 -11
- package/dist/{performance.js → performance.cjs} +23 -10
- package/dist/{plugins.js → plugins.cjs} +48 -47
- package/dist/plugins.d.ts +0 -6
- package/dist/plugins.d.ts.map +1 -1
- package/dist/{precision.js → precision.cjs} +74 -37
- package/dist/{rangePresets.js → rangePresets.cjs} +40 -19
- package/dist/{recurrence.js → recurrence.cjs} +27 -21
- package/dist/recurrence.d.ts.map +1 -1
- package/dist/scheduling.cjs +344 -0
- package/dist/scheduling.d.ts +206 -0
- package/dist/scheduling.d.ts.map +1 -0
- package/dist/{serialize.js → serialize.cjs} +36 -17
- package/dist/{temporal.js → temporal.cjs} +28 -13
- package/dist/{timezone.js → timezone.cjs} +140 -82
- package/dist/timezone.d.ts +6 -1
- package/dist/timezone.d.ts.map +1 -1
- package/dist/{types.js → types.cjs} +9 -3
- package/dist/types.d.ts +0 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/{validate.js → validate.cjs} +54 -26
- package/dist/{workingHours.js → workingHours.cjs} +36 -17
- package/package.json +52 -34
- package/dist/constants.js +0 -16
- package/dist/index.js +0 -66
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Finance utilities for market-aware date calculations
|
|
3
|
+
* Provides US market hours, trading days, settlement dates, and options expiration
|
|
4
|
+
*/
|
|
5
|
+
import type { DateInput } from './types.js';
|
|
6
|
+
/** Supported US stock markets */
|
|
7
|
+
export type USMarket = 'NYSE' | 'NASDAQ';
|
|
8
|
+
/** Market trading hours configuration */
|
|
9
|
+
export interface MarketHours {
|
|
10
|
+
/** Regular market open time */
|
|
11
|
+
open: {
|
|
12
|
+
hour: number;
|
|
13
|
+
minute: number;
|
|
14
|
+
};
|
|
15
|
+
/** Regular market close time */
|
|
16
|
+
close: {
|
|
17
|
+
hour: number;
|
|
18
|
+
minute: number;
|
|
19
|
+
};
|
|
20
|
+
/** Market timezone */
|
|
21
|
+
timezone: string;
|
|
22
|
+
/** Pre-market open time (optional) */
|
|
23
|
+
preMarket?: {
|
|
24
|
+
hour: number;
|
|
25
|
+
minute: number;
|
|
26
|
+
};
|
|
27
|
+
/** After-hours close time (optional) */
|
|
28
|
+
afterHours?: {
|
|
29
|
+
hour: number;
|
|
30
|
+
minute: number;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Options expiration type */
|
|
34
|
+
export type OptionsExpirationType = 'monthly' | 'weekly' | 'quarterly';
|
|
35
|
+
/** Market hours for US exchanges */
|
|
36
|
+
export declare const MARKET_HOURS: Record<USMarket, MarketHours>;
|
|
37
|
+
/** US market holidays (NYSE/NASDAQ follow same schedule) */
|
|
38
|
+
export declare const US_MARKET_HOLIDAYS: readonly ["New Year's Day", "Martin Luther King Jr. Day", "Presidents' Day", "Good Friday", "Memorial Day", "Juneteenth", "Independence Day", "Labor Day", "Thanksgiving Day", "Christmas Day"];
|
|
39
|
+
/**
|
|
40
|
+
* Check if a date is a US market holiday
|
|
41
|
+
* @param date - Date to check
|
|
42
|
+
* @param market - Market (default: NYSE)
|
|
43
|
+
* @returns True if the date is a market holiday
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* isMarketHoliday(new Date('2024-12-25')); // true (Christmas)
|
|
48
|
+
* isMarketHoliday(new Date('2024-01-02')); // false
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function isMarketHoliday(date: DateInput, market?: USMarket): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Check if a date is a trading day (weekday and not a market holiday)
|
|
54
|
+
* @param date - Date to check
|
|
55
|
+
* @param market - Market (default: NYSE)
|
|
56
|
+
* @returns True if the date is a trading day
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* isTradingDay(new Date('2024-01-15')); // true (Monday)
|
|
61
|
+
* isTradingDay(new Date('2024-01-13')); // false (Saturday)
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function isTradingDay(date: DateInput, market?: USMarket): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Check if the market is currently open
|
|
67
|
+
* @param date - Date/time to check
|
|
68
|
+
* @param market - Market (default: NYSE)
|
|
69
|
+
* @returns True if market is open at the specified time
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* // Check if NYSE is open now
|
|
74
|
+
* isMarketOpen(new Date(), 'NYSE');
|
|
75
|
+
*
|
|
76
|
+
* // Check specific time
|
|
77
|
+
* isMarketOpen(new Date('2024-01-15T10:30:00-05:00')); // true
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function isMarketOpen(date: DateInput, market?: USMarket): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Get market hours configuration
|
|
83
|
+
* @param market - Market (default: NYSE)
|
|
84
|
+
* @returns Market hours configuration (deep copy)
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* const hours = getMarketHours('NASDAQ');
|
|
89
|
+
* console.log(hours.open); // { hour: 9, minute: 30 }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function getMarketHours(market?: USMarket): MarketHours;
|
|
93
|
+
/**
|
|
94
|
+
* Get market open time for a specific date
|
|
95
|
+
* @param date - Date to get market open for
|
|
96
|
+
* @param market - Market (default: NYSE)
|
|
97
|
+
* @returns Date set to market open time
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const open = getMarketOpen(new Date('2024-01-15'));
|
|
102
|
+
* console.log(open); // 2024-01-15T09:30:00
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function getMarketOpen(date: DateInput, market?: USMarket): Date;
|
|
106
|
+
/**
|
|
107
|
+
* Get market close time for a specific date
|
|
108
|
+
* @param date - Date to get market close for
|
|
109
|
+
* @param market - Market (default: NYSE)
|
|
110
|
+
* @returns Date set to market close time
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* const close = getMarketClose(new Date('2024-01-15'));
|
|
115
|
+
* console.log(close); // 2024-01-15T16:00:00
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare function getMarketClose(date: DateInput, market?: USMarket): Date;
|
|
119
|
+
/**
|
|
120
|
+
* Get next market open time after a given date
|
|
121
|
+
* @param after - Start searching after this date
|
|
122
|
+
* @param market - Market (default: NYSE)
|
|
123
|
+
* @returns Next market open date/time
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* // If it's Friday evening, returns Monday 9:30 AM
|
|
128
|
+
* const nextOpen = getNextMarketOpen(new Date('2024-01-12T17:00:00'));
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export declare function getNextMarketOpen(after: DateInput, market?: USMarket): Date;
|
|
132
|
+
/**
|
|
133
|
+
* Get next market close time after a given date
|
|
134
|
+
* @param after - Start searching after this date
|
|
135
|
+
* @param market - Market (default: NYSE)
|
|
136
|
+
* @returns Next market close date/time
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* const nextClose = getNextMarketClose(new Date('2024-01-15T10:00:00'));
|
|
141
|
+
* // Returns 2024-01-15T16:00:00 (same day close)
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare function getNextMarketClose(after: DateInput, market?: USMarket): Date;
|
|
145
|
+
/**
|
|
146
|
+
* Calculate settlement date (T+N) from trade date
|
|
147
|
+
* @param tradeDate - Trade date
|
|
148
|
+
* @param days - Number of business days for settlement (e.g., 1 for T+1, 2 for T+2)
|
|
149
|
+
* @param market - Market (default: NYSE)
|
|
150
|
+
* @returns Settlement date
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* // T+2 settlement
|
|
155
|
+
* const settlement = getSettlementDate(new Date('2024-01-15'), 2);
|
|
156
|
+
* // Returns 2024-01-17 (skipping weekends/holidays)
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export declare function getSettlementDate(tradeDate: DateInput, days: number, market?: USMarket): Date;
|
|
160
|
+
/**
|
|
161
|
+
* Calculate trade date from settlement date (reverse T+N)
|
|
162
|
+
* @param settlementDate - Settlement date
|
|
163
|
+
* @param days - Number of business days for settlement
|
|
164
|
+
* @param market - Market (default: NYSE)
|
|
165
|
+
* @returns Trade date
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* const tradeDate = getTradeDateFromSettlement(new Date('2024-01-17'), 2);
|
|
170
|
+
* // Returns 2024-01-15
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export declare function getTradeDateFromSettlement(settlementDate: DateInput, days: number, market?: USMarket): Date;
|
|
174
|
+
/**
|
|
175
|
+
* Iterate through each trading day in a range
|
|
176
|
+
* @param start - Start date
|
|
177
|
+
* @param end - End date
|
|
178
|
+
* @param market - Market (default: NYSE)
|
|
179
|
+
* @returns Array of trading days
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* const days = eachTradingDay(new Date('2024-01-15'), new Date('2024-01-19'));
|
|
184
|
+
* // Returns Mon, Tue, Wed, Thu, Fri (if no holidays)
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export declare function eachTradingDay(start: DateInput, end: DateInput, market?: USMarket): Date[];
|
|
188
|
+
/**
|
|
189
|
+
* Count trading days between two dates
|
|
190
|
+
* @param start - Start date
|
|
191
|
+
* @param end - End date
|
|
192
|
+
* @param market - Market (default: NYSE)
|
|
193
|
+
* @returns Number of trading days
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* const count = countTradingDays(new Date('2024-01-15'), new Date('2024-01-19'));
|
|
198
|
+
* // Returns 5 (Mon-Fri if no holidays)
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export declare function countTradingDays(start: DateInput, end: DateInput, market?: USMarket): number;
|
|
202
|
+
/**
|
|
203
|
+
* Add trading days to a date
|
|
204
|
+
* @param date - Start date
|
|
205
|
+
* @param days - Number of trading days to add (can be negative)
|
|
206
|
+
* @param market - Market (default: NYSE)
|
|
207
|
+
* @returns Resulting date
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts
|
|
211
|
+
* const result = addTradingDays(new Date('2024-01-15'), 5);
|
|
212
|
+
* // Returns 5 trading days later
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
export declare function addTradingDays(date: DateInput, days: number, market?: USMarket): Date;
|
|
216
|
+
/**
|
|
217
|
+
* Get options expiration date
|
|
218
|
+
* @param year - Year
|
|
219
|
+
* @param month - Month (1-12)
|
|
220
|
+
* @param type - Expiration type (default: 'monthly')
|
|
221
|
+
* @returns Options expiration date
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* // Monthly options expire on 3rd Friday
|
|
226
|
+
* const exp = getOptionsExpiration(2024, 1, 'monthly');
|
|
227
|
+
*
|
|
228
|
+
* // Weekly options expire every Friday
|
|
229
|
+
* const weekly = getOptionsExpiration(2024, 1, 'weekly');
|
|
230
|
+
*
|
|
231
|
+
* // Quarterly options expire on 3rd Friday of Mar, Jun, Sep, Dec
|
|
232
|
+
* const quarterly = getOptionsExpiration(2024, 3, 'quarterly');
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export declare function getOptionsExpiration(year: number, month: number, type?: OptionsExpirationType): Date;
|
|
236
|
+
//# sourceMappingURL=finance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"finance.d.ts","sourceRoot":"","sources":["../src/finance.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,7 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* @fileoverview Fiscal year and accounting period utilities
|
|
3
4
|
* Supports configurable fiscal year start months for business calculations
|
|
4
5
|
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.FISCAL_PRESETS = void 0;
|
|
8
|
+
exports.getFiscalYear = getFiscalYear;
|
|
9
|
+
exports.getFiscalQuarter = getFiscalQuarter;
|
|
10
|
+
exports.getFiscalYearStart = getFiscalYearStart;
|
|
11
|
+
exports.getFiscalYearEnd = getFiscalYearEnd;
|
|
12
|
+
exports.getFiscalQuarterStart = getFiscalQuarterStart;
|
|
13
|
+
exports.getFiscalQuarterEnd = getFiscalQuarterEnd;
|
|
14
|
+
exports.isSameFiscalYear = isSameFiscalYear;
|
|
15
|
+
exports.isSameFiscalQuarter = isSameFiscalQuarter;
|
|
16
|
+
exports.getFiscalMonth = getFiscalMonth;
|
|
17
|
+
exports.getDaysRemainingInFiscalYear = getDaysRemainingInFiscalYear;
|
|
18
|
+
exports.getDaysElapsedInFiscalYear = getDaysElapsedInFiscalYear;
|
|
19
|
+
exports.getFiscalYearProgress = getFiscalYearProgress;
|
|
20
|
+
exports.getFiscalWeek = getFiscalWeek;
|
|
21
|
+
exports.formatFiscalYear = formatFiscalYear;
|
|
22
|
+
exports.formatFiscalQuarter = formatFiscalQuarter;
|
|
23
|
+
exports.getFiscalPeriodInfo = getFiscalPeriodInfo;
|
|
5
24
|
const DEFAULT_CONFIG = {
|
|
6
25
|
startMonth: 1, // January (calendar year)
|
|
7
26
|
};
|
|
@@ -22,7 +41,7 @@ const DEFAULT_CONFIG = {
|
|
|
22
41
|
* getFiscalYear(new Date('2024-06-15'), { startMonth: 7 }) // 2023
|
|
23
42
|
* getFiscalYear(new Date('2024-07-15'), { startMonth: 7 }) // 2024
|
|
24
43
|
*/
|
|
25
|
-
|
|
44
|
+
function getFiscalYear(date, config = {}) {
|
|
26
45
|
const { startMonth } = { ...DEFAULT_CONFIG, ...config };
|
|
27
46
|
const month = date.getMonth() + 1; // 1-12
|
|
28
47
|
const year = date.getFullYear();
|
|
@@ -47,7 +66,7 @@ export function getFiscalYear(date, config = {}) {
|
|
|
47
66
|
* getFiscalQuarter(new Date('2024-04-15'), { startMonth: 4 }) // 1
|
|
48
67
|
* getFiscalQuarter(new Date('2024-07-15'), { startMonth: 4 }) // 2
|
|
49
68
|
*/
|
|
50
|
-
|
|
69
|
+
function getFiscalQuarter(date, config = {}) {
|
|
51
70
|
const { startMonth } = { ...DEFAULT_CONFIG, ...config };
|
|
52
71
|
const month = date.getMonth() + 1; // 1-12
|
|
53
72
|
// Calculate months since fiscal year start
|
|
@@ -67,7 +86,7 @@ export function getFiscalQuarter(date, config = {}) {
|
|
|
67
86
|
* getFiscalYearStart(2024, { startMonth: 4 }) // 2024-04-01 (FY2024 starts Apr 2024)
|
|
68
87
|
* getFiscalYearStart(2024, { startMonth: 7 }) // 2024-07-01 (FY2024 starts Jul 2024)
|
|
69
88
|
*/
|
|
70
|
-
|
|
89
|
+
function getFiscalYearStart(fiscalYear, config = {}) {
|
|
71
90
|
const { startMonth } = { ...DEFAULT_CONFIG, ...config };
|
|
72
91
|
// Fiscal year N starts in the startMonth of calendar year N
|
|
73
92
|
return new Date(fiscalYear, startMonth - 1, 1);
|
|
@@ -82,7 +101,7 @@ export function getFiscalYearStart(fiscalYear, config = {}) {
|
|
|
82
101
|
* getFiscalYearEnd(2024, { startMonth: 4 }) // 2025-03-31 (FY2024 ends Mar 2025)
|
|
83
102
|
* getFiscalYearEnd(2024, { startMonth: 7 }) // 2025-06-30 (FY2024 ends Jun 2025)
|
|
84
103
|
*/
|
|
85
|
-
|
|
104
|
+
function getFiscalYearEnd(fiscalYear, config = {}) {
|
|
86
105
|
const { startMonth } = { ...DEFAULT_CONFIG, ...config };
|
|
87
106
|
if (startMonth === 1) {
|
|
88
107
|
return new Date(fiscalYear, 11, 31, 23, 59, 59, 999);
|
|
@@ -107,7 +126,7 @@ export function getFiscalYearEnd(fiscalYear, config = {}) {
|
|
|
107
126
|
* getFiscalQuarterStart(2024, 1, { startMonth: 4 }) // 2023-04-01
|
|
108
127
|
* getFiscalQuarterStart(2024, 2, { startMonth: 4 }) // 2023-07-01
|
|
109
128
|
*/
|
|
110
|
-
|
|
129
|
+
function getFiscalQuarterStart(fiscalYear, quarter, config = {}) {
|
|
111
130
|
if (quarter < 1 || quarter > 4) {
|
|
112
131
|
throw new Error('Quarter must be between 1 and 4');
|
|
113
132
|
}
|
|
@@ -129,7 +148,7 @@ export function getFiscalQuarterStart(fiscalYear, quarter, config = {}) {
|
|
|
129
148
|
* getFiscalQuarterEnd(2024, 2) // 2024-06-30
|
|
130
149
|
* getFiscalQuarterEnd(2024, 1, { startMonth: 4 }) // 2023-06-30
|
|
131
150
|
*/
|
|
132
|
-
|
|
151
|
+
function getFiscalQuarterEnd(fiscalYear, quarter, config = {}) {
|
|
133
152
|
if (quarter < 1 || quarter > 4) {
|
|
134
153
|
throw new Error('Quarter must be between 1 and 4');
|
|
135
154
|
}
|
|
@@ -146,7 +165,7 @@ export function getFiscalQuarterEnd(fiscalYear, quarter, config = {}) {
|
|
|
146
165
|
* @param config - Fiscal year configuration
|
|
147
166
|
* @returns True if both dates are in the same fiscal year
|
|
148
167
|
*/
|
|
149
|
-
|
|
168
|
+
function isSameFiscalYear(date1, date2, config = {}) {
|
|
150
169
|
return getFiscalYear(date1, config) === getFiscalYear(date2, config);
|
|
151
170
|
}
|
|
152
171
|
/**
|
|
@@ -156,7 +175,7 @@ export function isSameFiscalYear(date1, date2, config = {}) {
|
|
|
156
175
|
* @param config - Fiscal year configuration
|
|
157
176
|
* @returns True if both dates are in the same fiscal year and quarter
|
|
158
177
|
*/
|
|
159
|
-
|
|
178
|
+
function isSameFiscalQuarter(date1, date2, config = {}) {
|
|
160
179
|
return (getFiscalYear(date1, config) === getFiscalYear(date2, config) &&
|
|
161
180
|
getFiscalQuarter(date1, config) === getFiscalQuarter(date2, config));
|
|
162
181
|
}
|
|
@@ -170,7 +189,7 @@ export function isSameFiscalQuarter(date1, date2, config = {}) {
|
|
|
170
189
|
* getFiscalMonth(new Date('2024-04-15'), { startMonth: 4 }) // 1
|
|
171
190
|
* getFiscalMonth(new Date('2024-03-15'), { startMonth: 4 }) // 12
|
|
172
191
|
*/
|
|
173
|
-
|
|
192
|
+
function getFiscalMonth(date, config = {}) {
|
|
174
193
|
const { startMonth } = { ...DEFAULT_CONFIG, ...config };
|
|
175
194
|
const month = date.getMonth() + 1; // 1-12
|
|
176
195
|
let fiscalMonth = month - startMonth + 1;
|
|
@@ -185,7 +204,7 @@ export function getFiscalMonth(date, config = {}) {
|
|
|
185
204
|
* @param config - Fiscal year configuration
|
|
186
205
|
* @returns Number of days remaining in the fiscal year
|
|
187
206
|
*/
|
|
188
|
-
|
|
207
|
+
function getDaysRemainingInFiscalYear(date, config = {}) {
|
|
189
208
|
const fiscalYear = getFiscalYear(date, config);
|
|
190
209
|
const fyEnd = getFiscalYearEnd(fiscalYear, config);
|
|
191
210
|
const startOfDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
@@ -198,7 +217,7 @@ export function getDaysRemainingInFiscalYear(date, config = {}) {
|
|
|
198
217
|
* @param config - Fiscal year configuration
|
|
199
218
|
* @returns Number of days elapsed in the fiscal year (including the given date)
|
|
200
219
|
*/
|
|
201
|
-
|
|
220
|
+
function getDaysElapsedInFiscalYear(date, config = {}) {
|
|
202
221
|
const fiscalYear = getFiscalYear(date, config);
|
|
203
222
|
const fyStart = getFiscalYearStart(fiscalYear, config);
|
|
204
223
|
const startOfDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
@@ -211,7 +230,7 @@ export function getDaysElapsedInFiscalYear(date, config = {}) {
|
|
|
211
230
|
* @param config - Fiscal year configuration
|
|
212
231
|
* @returns Percentage of fiscal year completed (0-100)
|
|
213
232
|
*/
|
|
214
|
-
|
|
233
|
+
function getFiscalYearProgress(date, config = {}) {
|
|
215
234
|
const fiscalYear = getFiscalYear(date, config);
|
|
216
235
|
const fyStart = getFiscalYearStart(fiscalYear, config);
|
|
217
236
|
const fyEnd = getFiscalYearEnd(fiscalYear, config);
|
|
@@ -225,14 +244,14 @@ export function getFiscalYearProgress(date, config = {}) {
|
|
|
225
244
|
* @param config - Fiscal year configuration
|
|
226
245
|
* @returns The fiscal week number
|
|
227
246
|
*/
|
|
228
|
-
|
|
247
|
+
function getFiscalWeek(date, config = {}) {
|
|
229
248
|
const elapsed = getDaysElapsedInFiscalYear(date, config);
|
|
230
249
|
return Math.ceil(elapsed / 7);
|
|
231
250
|
}
|
|
232
251
|
/**
|
|
233
252
|
* Common fiscal year configurations
|
|
234
253
|
*/
|
|
235
|
-
|
|
254
|
+
exports.FISCAL_PRESETS = {
|
|
236
255
|
/** Calendar year (January start) - Default */
|
|
237
256
|
CALENDAR: { startMonth: 1 },
|
|
238
257
|
/** UK/India government fiscal year (April start) */
|
|
@@ -249,7 +268,7 @@ export const FISCAL_PRESETS = {
|
|
|
249
268
|
* @param format - Format style: 'short' (FY2024) or 'long' (FY2023/24)
|
|
250
269
|
* @returns Formatted fiscal year string
|
|
251
270
|
*/
|
|
252
|
-
|
|
271
|
+
function formatFiscalYear(fiscalYear, config = {}, format = 'short') {
|
|
253
272
|
const { startMonth } = { ...DEFAULT_CONFIG, ...config };
|
|
254
273
|
if (format === 'short' || startMonth === 1) {
|
|
255
274
|
return `FY${fiscalYear}`;
|
|
@@ -266,7 +285,7 @@ export function formatFiscalYear(fiscalYear, config = {}, format = 'short') {
|
|
|
266
285
|
* @param config - Fiscal year configuration
|
|
267
286
|
* @returns Formatted fiscal quarter string
|
|
268
287
|
*/
|
|
269
|
-
|
|
288
|
+
function formatFiscalQuarter(fiscalYear, quarter, config = {}) {
|
|
270
289
|
const fyString = formatFiscalYear(fiscalYear, config, 'short');
|
|
271
290
|
return `Q${quarter} ${fyString}`;
|
|
272
291
|
}
|
|
@@ -276,7 +295,7 @@ export function formatFiscalQuarter(fiscalYear, quarter, config = {}) {
|
|
|
276
295
|
* @param config - Fiscal year configuration
|
|
277
296
|
* @returns Object with fiscal period information
|
|
278
297
|
*/
|
|
279
|
-
|
|
298
|
+
function getFiscalPeriodInfo(date, config = {}) {
|
|
280
299
|
const fiscalYear = getFiscalYear(date, config);
|
|
281
300
|
const fiscalQuarter = getFiscalQuarter(date, config);
|
|
282
301
|
return {
|