ts-time-utils 1.0.0 → 2.0.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.
Files changed (102) hide show
  1. package/README.md +682 -11
  2. package/dist/calculate.d.ts +7 -2
  3. package/dist/calculate.d.ts.map +1 -1
  4. package/dist/calculate.js +37 -13
  5. package/dist/calendar.d.ts +103 -0
  6. package/dist/calendar.d.ts.map +1 -1
  7. package/dist/calendar.js +224 -0
  8. package/dist/compare.d.ts +217 -0
  9. package/dist/compare.d.ts.map +1 -0
  10. package/dist/compare.js +417 -0
  11. package/dist/countdown.d.ts +217 -0
  12. package/dist/countdown.d.ts.map +1 -0
  13. package/dist/countdown.js +298 -0
  14. package/dist/cron.d.ts +82 -0
  15. package/dist/cron.d.ts.map +1 -0
  16. package/dist/cron.js +294 -0
  17. package/dist/dateRange.d.ts +266 -0
  18. package/dist/dateRange.d.ts.map +1 -0
  19. package/dist/dateRange.js +433 -0
  20. package/dist/esm/calculate.d.ts +7 -2
  21. package/dist/esm/calculate.d.ts.map +1 -1
  22. package/dist/esm/calculate.js +37 -13
  23. package/dist/esm/calendar.d.ts +103 -0
  24. package/dist/esm/calendar.d.ts.map +1 -1
  25. package/dist/esm/calendar.js +224 -0
  26. package/dist/esm/compare.d.ts +217 -0
  27. package/dist/esm/compare.d.ts.map +1 -0
  28. package/dist/esm/compare.js +417 -0
  29. package/dist/esm/countdown.d.ts +217 -0
  30. package/dist/esm/countdown.d.ts.map +1 -0
  31. package/dist/esm/countdown.js +298 -0
  32. package/dist/esm/cron.d.ts +82 -0
  33. package/dist/esm/cron.d.ts.map +1 -0
  34. package/dist/esm/cron.js +294 -0
  35. package/dist/esm/dateRange.d.ts +266 -0
  36. package/dist/esm/dateRange.d.ts.map +1 -0
  37. package/dist/esm/dateRange.js +433 -0
  38. package/dist/esm/fiscal.d.ts +195 -0
  39. package/dist/esm/fiscal.d.ts.map +1 -0
  40. package/dist/esm/fiscal.js +295 -0
  41. package/dist/esm/format.d.ts +65 -0
  42. package/dist/esm/format.d.ts.map +1 -1
  43. package/dist/esm/format.js +202 -0
  44. package/dist/esm/index.d.ts +18 -7
  45. package/dist/esm/index.d.ts.map +1 -1
  46. package/dist/esm/index.js +22 -6
  47. package/dist/esm/iterate.d.ts +212 -0
  48. package/dist/esm/iterate.d.ts.map +1 -0
  49. package/dist/esm/iterate.js +409 -0
  50. package/dist/esm/naturalLanguage.d.ts +107 -0
  51. package/dist/esm/naturalLanguage.d.ts.map +1 -0
  52. package/dist/esm/naturalLanguage.js +344 -0
  53. package/dist/esm/parse.d.ts +45 -0
  54. package/dist/esm/parse.d.ts.map +1 -1
  55. package/dist/esm/parse.js +207 -0
  56. package/dist/esm/recurrence.d.ts +149 -0
  57. package/dist/esm/recurrence.d.ts.map +1 -0
  58. package/dist/esm/recurrence.js +404 -0
  59. package/dist/esm/timezone.d.ts +52 -0
  60. package/dist/esm/timezone.d.ts.map +1 -1
  61. package/dist/esm/timezone.js +171 -0
  62. package/dist/esm/types.d.ts +21 -0
  63. package/dist/esm/types.d.ts.map +1 -1
  64. package/dist/esm/validate.d.ts +51 -0
  65. package/dist/esm/validate.d.ts.map +1 -1
  66. package/dist/esm/validate.js +92 -0
  67. package/dist/esm/workingHours.d.ts +70 -0
  68. package/dist/esm/workingHours.d.ts.map +1 -1
  69. package/dist/esm/workingHours.js +161 -0
  70. package/dist/fiscal.d.ts +195 -0
  71. package/dist/fiscal.d.ts.map +1 -0
  72. package/dist/fiscal.js +295 -0
  73. package/dist/format.d.ts +65 -0
  74. package/dist/format.d.ts.map +1 -1
  75. package/dist/format.js +202 -0
  76. package/dist/index.d.ts +18 -7
  77. package/dist/index.d.ts.map +1 -1
  78. package/dist/index.js +22 -6
  79. package/dist/iterate.d.ts +212 -0
  80. package/dist/iterate.d.ts.map +1 -0
  81. package/dist/iterate.js +409 -0
  82. package/dist/naturalLanguage.d.ts +107 -0
  83. package/dist/naturalLanguage.d.ts.map +1 -0
  84. package/dist/naturalLanguage.js +344 -0
  85. package/dist/parse.d.ts +45 -0
  86. package/dist/parse.d.ts.map +1 -1
  87. package/dist/parse.js +207 -0
  88. package/dist/recurrence.d.ts +149 -0
  89. package/dist/recurrence.d.ts.map +1 -0
  90. package/dist/recurrence.js +404 -0
  91. package/dist/timezone.d.ts +52 -0
  92. package/dist/timezone.d.ts.map +1 -1
  93. package/dist/timezone.js +171 -0
  94. package/dist/types.d.ts +21 -0
  95. package/dist/types.d.ts.map +1 -1
  96. package/dist/validate.d.ts +51 -0
  97. package/dist/validate.d.ts.map +1 -1
  98. package/dist/validate.js +92 -0
  99. package/dist/workingHours.d.ts +70 -0
  100. package/dist/workingHours.d.ts.map +1 -1
  101. package/dist/workingHours.js +161 -0
  102. package/package.json +59 -12
@@ -0,0 +1,195 @@
1
+ /**
2
+ * @fileoverview Fiscal year and accounting period utilities
3
+ * Supports configurable fiscal year start months for business calculations
4
+ */
5
+ /**
6
+ * Fiscal year configuration
7
+ */
8
+ export interface FiscalConfig {
9
+ /** Month when fiscal year starts (1-12, default: 1 for January) */
10
+ startMonth: number;
11
+ }
12
+ /**
13
+ * Get the fiscal year for a given date
14
+ * @param date - The date to check
15
+ * @param config - Fiscal year configuration
16
+ * @returns The fiscal year number
17
+ * @example
18
+ * // Calendar year (Jan start)
19
+ * getFiscalYear(new Date('2024-03-15')) // 2024
20
+ *
21
+ * // April fiscal year (UK/India style)
22
+ * getFiscalYear(new Date('2024-03-15'), { startMonth: 4 }) // 2023
23
+ * getFiscalYear(new Date('2024-04-15'), { startMonth: 4 }) // 2024
24
+ *
25
+ * // July fiscal year (Australia style)
26
+ * getFiscalYear(new Date('2024-06-15'), { startMonth: 7 }) // 2023
27
+ * getFiscalYear(new Date('2024-07-15'), { startMonth: 7 }) // 2024
28
+ */
29
+ export declare function getFiscalYear(date: Date, config?: Partial<FiscalConfig>): number;
30
+ /**
31
+ * Get the fiscal quarter for a given date (1-4)
32
+ * @param date - The date to check
33
+ * @param config - Fiscal year configuration
34
+ * @returns The fiscal quarter (1-4)
35
+ * @example
36
+ * // Calendar year quarters
37
+ * getFiscalQuarter(new Date('2024-01-15')) // 1
38
+ * getFiscalQuarter(new Date('2024-04-15')) // 2
39
+ *
40
+ * // April fiscal year
41
+ * getFiscalQuarter(new Date('2024-04-15'), { startMonth: 4 }) // 1
42
+ * getFiscalQuarter(new Date('2024-07-15'), { startMonth: 4 }) // 2
43
+ */
44
+ export declare function getFiscalQuarter(date: Date, config?: Partial<FiscalConfig>): number;
45
+ /**
46
+ * Get the start date of a fiscal year
47
+ * @param fiscalYear - The fiscal year
48
+ * @param config - Fiscal year configuration
49
+ * @returns Start date of the fiscal year
50
+ * @example
51
+ * getFiscalYearStart(2024) // 2024-01-01
52
+ * getFiscalYearStart(2024, { startMonth: 4 }) // 2024-04-01 (FY2024 starts Apr 2024)
53
+ * getFiscalYearStart(2024, { startMonth: 7 }) // 2024-07-01 (FY2024 starts Jul 2024)
54
+ */
55
+ export declare function getFiscalYearStart(fiscalYear: number, config?: Partial<FiscalConfig>): Date;
56
+ /**
57
+ * Get the end date of a fiscal year
58
+ * @param fiscalYear - The fiscal year
59
+ * @param config - Fiscal year configuration
60
+ * @returns End date of the fiscal year (last day, 23:59:59.999)
61
+ * @example
62
+ * getFiscalYearEnd(2024) // 2024-12-31
63
+ * getFiscalYearEnd(2024, { startMonth: 4 }) // 2025-03-31 (FY2024 ends Mar 2025)
64
+ * getFiscalYearEnd(2024, { startMonth: 7 }) // 2025-06-30 (FY2024 ends Jun 2025)
65
+ */
66
+ export declare function getFiscalYearEnd(fiscalYear: number, config?: Partial<FiscalConfig>): Date;
67
+ /**
68
+ * Get the start date of a fiscal quarter
69
+ * @param fiscalYear - The fiscal year
70
+ * @param quarter - The quarter (1-4)
71
+ * @param config - Fiscal year configuration
72
+ * @returns Start date of the fiscal quarter
73
+ * @example
74
+ * getFiscalQuarterStart(2024, 1) // 2024-01-01
75
+ * getFiscalQuarterStart(2024, 2) // 2024-04-01
76
+ * getFiscalQuarterStart(2024, 1, { startMonth: 4 }) // 2023-04-01
77
+ * getFiscalQuarterStart(2024, 2, { startMonth: 4 }) // 2023-07-01
78
+ */
79
+ export declare function getFiscalQuarterStart(fiscalYear: number, quarter: number, config?: Partial<FiscalConfig>): Date;
80
+ /**
81
+ * Get the end date of a fiscal quarter
82
+ * @param fiscalYear - The fiscal year
83
+ * @param quarter - The quarter (1-4)
84
+ * @param config - Fiscal year configuration
85
+ * @returns End date of the fiscal quarter (last day, 23:59:59.999)
86
+ * @example
87
+ * getFiscalQuarterEnd(2024, 1) // 2024-03-31
88
+ * getFiscalQuarterEnd(2024, 2) // 2024-06-30
89
+ * getFiscalQuarterEnd(2024, 1, { startMonth: 4 }) // 2023-06-30
90
+ */
91
+ export declare function getFiscalQuarterEnd(fiscalYear: number, quarter: number, config?: Partial<FiscalConfig>): Date;
92
+ /**
93
+ * Check if two dates are in the same fiscal year
94
+ * @param date1 - First date
95
+ * @param date2 - Second date
96
+ * @param config - Fiscal year configuration
97
+ * @returns True if both dates are in the same fiscal year
98
+ */
99
+ export declare function isSameFiscalYear(date1: Date, date2: Date, config?: Partial<FiscalConfig>): boolean;
100
+ /**
101
+ * Check if two dates are in the same fiscal quarter
102
+ * @param date1 - First date
103
+ * @param date2 - Second date
104
+ * @param config - Fiscal year configuration
105
+ * @returns True if both dates are in the same fiscal year and quarter
106
+ */
107
+ export declare function isSameFiscalQuarter(date1: Date, date2: Date, config?: Partial<FiscalConfig>): boolean;
108
+ /**
109
+ * Get the fiscal month (1-12) within the fiscal year
110
+ * @param date - The date to check
111
+ * @param config - Fiscal year configuration
112
+ * @returns The fiscal month (1-12, where 1 is the first month of fiscal year)
113
+ * @example
114
+ * getFiscalMonth(new Date('2024-01-15')) // 1
115
+ * getFiscalMonth(new Date('2024-04-15'), { startMonth: 4 }) // 1
116
+ * getFiscalMonth(new Date('2024-03-15'), { startMonth: 4 }) // 12
117
+ */
118
+ export declare function getFiscalMonth(date: Date, config?: Partial<FiscalConfig>): number;
119
+ /**
120
+ * Get the number of days remaining in the fiscal year
121
+ * @param date - The date to check
122
+ * @param config - Fiscal year configuration
123
+ * @returns Number of days remaining in the fiscal year
124
+ */
125
+ export declare function getDaysRemainingInFiscalYear(date: Date, config?: Partial<FiscalConfig>): number;
126
+ /**
127
+ * Get the number of days elapsed in the fiscal year
128
+ * @param date - The date to check
129
+ * @param config - Fiscal year configuration
130
+ * @returns Number of days elapsed in the fiscal year (including the given date)
131
+ */
132
+ export declare function getDaysElapsedInFiscalYear(date: Date, config?: Partial<FiscalConfig>): number;
133
+ /**
134
+ * Get fiscal year progress as a percentage
135
+ * @param date - The date to check
136
+ * @param config - Fiscal year configuration
137
+ * @returns Percentage of fiscal year completed (0-100)
138
+ */
139
+ export declare function getFiscalYearProgress(date: Date, config?: Partial<FiscalConfig>): number;
140
+ /**
141
+ * Get the fiscal week number (1-53) within the fiscal year
142
+ * @param date - The date to check
143
+ * @param config - Fiscal year configuration
144
+ * @returns The fiscal week number
145
+ */
146
+ export declare function getFiscalWeek(date: Date, config?: Partial<FiscalConfig>): number;
147
+ /**
148
+ * Common fiscal year configurations
149
+ */
150
+ export declare const FISCAL_PRESETS: {
151
+ /** Calendar year (January start) - Default */
152
+ readonly CALENDAR: FiscalConfig;
153
+ /** UK/India government fiscal year (April start) */
154
+ readonly UK_INDIA: FiscalConfig;
155
+ /** Australian fiscal year (July start) */
156
+ readonly AUSTRALIA: FiscalConfig;
157
+ /** US federal government fiscal year (October start) */
158
+ readonly US_FEDERAL: FiscalConfig;
159
+ };
160
+ /**
161
+ * Format fiscal year as string (e.g., "FY2024" or "FY2023/24")
162
+ * @param fiscalYear - The fiscal year
163
+ * @param config - Fiscal year configuration
164
+ * @param format - Format style: 'short' (FY2024) or 'long' (FY2023/24)
165
+ * @returns Formatted fiscal year string
166
+ */
167
+ export declare function formatFiscalYear(fiscalYear: number, config?: Partial<FiscalConfig>, format?: 'short' | 'long'): string;
168
+ /**
169
+ * Format fiscal quarter as string (e.g., "Q1 FY2024")
170
+ * @param fiscalYear - The fiscal year
171
+ * @param quarter - The quarter (1-4)
172
+ * @param config - Fiscal year configuration
173
+ * @returns Formatted fiscal quarter string
174
+ */
175
+ export declare function formatFiscalQuarter(fiscalYear: number, quarter: number, config?: Partial<FiscalConfig>): string;
176
+ /**
177
+ * Get fiscal period info for a date
178
+ * @param date - The date to analyze
179
+ * @param config - Fiscal year configuration
180
+ * @returns Object with fiscal period information
181
+ */
182
+ export declare function getFiscalPeriodInfo(date: Date, config?: Partial<FiscalConfig>): {
183
+ fiscalYear: number;
184
+ fiscalQuarter: number;
185
+ fiscalMonth: number;
186
+ fiscalWeek: number;
187
+ daysElapsed: number;
188
+ daysRemaining: number;
189
+ progress: number;
190
+ quarterStart: Date;
191
+ quarterEnd: Date;
192
+ yearStart: Date;
193
+ yearEnd: Date;
194
+ };
195
+ //# sourceMappingURL=fiscal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fiscal.d.ts","sourceRoot":"","sources":["../src/fiscal.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,mEAAmE;IACnE,UAAU,EAAE,MAAM,CAAC;CACpB;AAMD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,IAAI,EACV,MAAM,GAAE,OAAO,CAAC,YAAY,CAAM,GACjC,MAAM,CAYR;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,IAAI,EACV,MAAM,GAAE,OAAO,CAAC,YAAY,CAAM,GACjC,MAAM,CAWR;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,OAAO,CAAC,YAAY,CAAM,GACjC,IAAI,CAIN;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,OAAO,CA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ED;;GAEG;AACH,eAAO,MAAM,cAAc;IACzB,8CAA8C;uBACf,YAAY;IAC3C,oDAAoD;uBACrB,YAAY;IAC3C,0CAA0C;wBACV,YAAY;IAC5C,wDAAwD;yBACtB,YAAY;CACtC,CAAC;AAEX;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,OAAO,CAAC,YAAY,CAAM,EAClC,MAAM,GAAE,OAAO,GAAG,MAAgB,GACjC,MAAM,CAWR;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,MAAM,GAAE,OAAO,CAAC,YAAY,CAAM,GACjC,MAAM,CAGR;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,IAAI,EACV,MAAM,GAAE,OAAO,CAAC,YAAY,CAAM,GACjC;IACD,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,IAAI,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;IACjB,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,EAAE,IAAI,CAAC;CACf,CAiBA"}
package/dist/fiscal.js ADDED
@@ -0,0 +1,295 @@
1
+ /**
2
+ * @fileoverview Fiscal year and accounting period utilities
3
+ * Supports configurable fiscal year start months for business calculations
4
+ */
5
+ const DEFAULT_CONFIG = {
6
+ startMonth: 1, // January (calendar year)
7
+ };
8
+ /**
9
+ * Get the fiscal year for a given date
10
+ * @param date - The date to check
11
+ * @param config - Fiscal year configuration
12
+ * @returns The fiscal year number
13
+ * @example
14
+ * // Calendar year (Jan start)
15
+ * getFiscalYear(new Date('2024-03-15')) // 2024
16
+ *
17
+ * // April fiscal year (UK/India style)
18
+ * getFiscalYear(new Date('2024-03-15'), { startMonth: 4 }) // 2023
19
+ * getFiscalYear(new Date('2024-04-15'), { startMonth: 4 }) // 2024
20
+ *
21
+ * // July fiscal year (Australia style)
22
+ * getFiscalYear(new Date('2024-06-15'), { startMonth: 7 }) // 2023
23
+ * getFiscalYear(new Date('2024-07-15'), { startMonth: 7 }) // 2024
24
+ */
25
+ export function getFiscalYear(date, config = {}) {
26
+ const { startMonth } = { ...DEFAULT_CONFIG, ...config };
27
+ const month = date.getMonth() + 1; // 1-12
28
+ const year = date.getFullYear();
29
+ if (startMonth === 1) {
30
+ return year;
31
+ }
32
+ // Fiscal year is named by when it starts
33
+ // If current month is before fiscal year start, we're still in the fiscal year that started last calendar year
34
+ return month < startMonth ? year - 1 : year;
35
+ }
36
+ /**
37
+ * Get the fiscal quarter for a given date (1-4)
38
+ * @param date - The date to check
39
+ * @param config - Fiscal year configuration
40
+ * @returns The fiscal quarter (1-4)
41
+ * @example
42
+ * // Calendar year quarters
43
+ * getFiscalQuarter(new Date('2024-01-15')) // 1
44
+ * getFiscalQuarter(new Date('2024-04-15')) // 2
45
+ *
46
+ * // April fiscal year
47
+ * getFiscalQuarter(new Date('2024-04-15'), { startMonth: 4 }) // 1
48
+ * getFiscalQuarter(new Date('2024-07-15'), { startMonth: 4 }) // 2
49
+ */
50
+ export function getFiscalQuarter(date, config = {}) {
51
+ const { startMonth } = { ...DEFAULT_CONFIG, ...config };
52
+ const month = date.getMonth() + 1; // 1-12
53
+ // Calculate months since fiscal year start
54
+ let monthsIntoFiscalYear = month - startMonth;
55
+ if (monthsIntoFiscalYear < 0) {
56
+ monthsIntoFiscalYear += 12;
57
+ }
58
+ return Math.floor(monthsIntoFiscalYear / 3) + 1;
59
+ }
60
+ /**
61
+ * Get the start date of a fiscal year
62
+ * @param fiscalYear - The fiscal year
63
+ * @param config - Fiscal year configuration
64
+ * @returns Start date of the fiscal year
65
+ * @example
66
+ * getFiscalYearStart(2024) // 2024-01-01
67
+ * getFiscalYearStart(2024, { startMonth: 4 }) // 2024-04-01 (FY2024 starts Apr 2024)
68
+ * getFiscalYearStart(2024, { startMonth: 7 }) // 2024-07-01 (FY2024 starts Jul 2024)
69
+ */
70
+ export function getFiscalYearStart(fiscalYear, config = {}) {
71
+ const { startMonth } = { ...DEFAULT_CONFIG, ...config };
72
+ // Fiscal year N starts in the startMonth of calendar year N
73
+ return new Date(fiscalYear, startMonth - 1, 1);
74
+ }
75
+ /**
76
+ * Get the end date of a fiscal year
77
+ * @param fiscalYear - The fiscal year
78
+ * @param config - Fiscal year configuration
79
+ * @returns End date of the fiscal year (last day, 23:59:59.999)
80
+ * @example
81
+ * getFiscalYearEnd(2024) // 2024-12-31
82
+ * getFiscalYearEnd(2024, { startMonth: 4 }) // 2025-03-31 (FY2024 ends Mar 2025)
83
+ * getFiscalYearEnd(2024, { startMonth: 7 }) // 2025-06-30 (FY2024 ends Jun 2025)
84
+ */
85
+ export function getFiscalYearEnd(fiscalYear, config = {}) {
86
+ const { startMonth } = { ...DEFAULT_CONFIG, ...config };
87
+ if (startMonth === 1) {
88
+ return new Date(fiscalYear, 11, 31, 23, 59, 59, 999);
89
+ }
90
+ // FY ends the day before the next FY starts
91
+ // FY N ends in (startMonth - 1) of year (N + 1)
92
+ const endMonth = startMonth - 1; // Month before start month (0-indexed: 0=Jan...11=Dec)
93
+ const endYear = fiscalYear + 1;
94
+ // Get last day of the end month
95
+ const lastDay = new Date(endYear, endMonth, 0).getDate();
96
+ return new Date(endYear, endMonth - 1, lastDay, 23, 59, 59, 999);
97
+ }
98
+ /**
99
+ * Get the start date of a fiscal quarter
100
+ * @param fiscalYear - The fiscal year
101
+ * @param quarter - The quarter (1-4)
102
+ * @param config - Fiscal year configuration
103
+ * @returns Start date of the fiscal quarter
104
+ * @example
105
+ * getFiscalQuarterStart(2024, 1) // 2024-01-01
106
+ * getFiscalQuarterStart(2024, 2) // 2024-04-01
107
+ * getFiscalQuarterStart(2024, 1, { startMonth: 4 }) // 2023-04-01
108
+ * getFiscalQuarterStart(2024, 2, { startMonth: 4 }) // 2023-07-01
109
+ */
110
+ export function getFiscalQuarterStart(fiscalYear, quarter, config = {}) {
111
+ if (quarter < 1 || quarter > 4) {
112
+ throw new Error('Quarter must be between 1 and 4');
113
+ }
114
+ const { startMonth } = { ...DEFAULT_CONFIG, ...config };
115
+ const fyStart = getFiscalYearStart(fiscalYear, config);
116
+ // Add (quarter - 1) * 3 months to fiscal year start
117
+ const monthsToAdd = (quarter - 1) * 3;
118
+ const resultMonth = fyStart.getMonth() + monthsToAdd;
119
+ return new Date(fyStart.getFullYear(), resultMonth, 1);
120
+ }
121
+ /**
122
+ * Get the end date of a fiscal quarter
123
+ * @param fiscalYear - The fiscal year
124
+ * @param quarter - The quarter (1-4)
125
+ * @param config - Fiscal year configuration
126
+ * @returns End date of the fiscal quarter (last day, 23:59:59.999)
127
+ * @example
128
+ * getFiscalQuarterEnd(2024, 1) // 2024-03-31
129
+ * getFiscalQuarterEnd(2024, 2) // 2024-06-30
130
+ * getFiscalQuarterEnd(2024, 1, { startMonth: 4 }) // 2023-06-30
131
+ */
132
+ export function getFiscalQuarterEnd(fiscalYear, quarter, config = {}) {
133
+ if (quarter < 1 || quarter > 4) {
134
+ throw new Error('Quarter must be between 1 and 4');
135
+ }
136
+ const quarterStart = getFiscalQuarterStart(fiscalYear, quarter, config);
137
+ // End of quarter is last day of the third month
138
+ const endMonth = quarterStart.getMonth() + 3;
139
+ const lastDay = new Date(quarterStart.getFullYear(), endMonth, 0).getDate();
140
+ return new Date(quarterStart.getFullYear(), endMonth - 1, lastDay, 23, 59, 59, 999);
141
+ }
142
+ /**
143
+ * Check if two dates are in the same fiscal year
144
+ * @param date1 - First date
145
+ * @param date2 - Second date
146
+ * @param config - Fiscal year configuration
147
+ * @returns True if both dates are in the same fiscal year
148
+ */
149
+ export function isSameFiscalYear(date1, date2, config = {}) {
150
+ return getFiscalYear(date1, config) === getFiscalYear(date2, config);
151
+ }
152
+ /**
153
+ * Check if two dates are in the same fiscal quarter
154
+ * @param date1 - First date
155
+ * @param date2 - Second date
156
+ * @param config - Fiscal year configuration
157
+ * @returns True if both dates are in the same fiscal year and quarter
158
+ */
159
+ export function isSameFiscalQuarter(date1, date2, config = {}) {
160
+ return (getFiscalYear(date1, config) === getFiscalYear(date2, config) &&
161
+ getFiscalQuarter(date1, config) === getFiscalQuarter(date2, config));
162
+ }
163
+ /**
164
+ * Get the fiscal month (1-12) within the fiscal year
165
+ * @param date - The date to check
166
+ * @param config - Fiscal year configuration
167
+ * @returns The fiscal month (1-12, where 1 is the first month of fiscal year)
168
+ * @example
169
+ * getFiscalMonth(new Date('2024-01-15')) // 1
170
+ * getFiscalMonth(new Date('2024-04-15'), { startMonth: 4 }) // 1
171
+ * getFiscalMonth(new Date('2024-03-15'), { startMonth: 4 }) // 12
172
+ */
173
+ export function getFiscalMonth(date, config = {}) {
174
+ const { startMonth } = { ...DEFAULT_CONFIG, ...config };
175
+ const month = date.getMonth() + 1; // 1-12
176
+ let fiscalMonth = month - startMonth + 1;
177
+ if (fiscalMonth <= 0) {
178
+ fiscalMonth += 12;
179
+ }
180
+ return fiscalMonth;
181
+ }
182
+ /**
183
+ * Get the number of days remaining in the fiscal year
184
+ * @param date - The date to check
185
+ * @param config - Fiscal year configuration
186
+ * @returns Number of days remaining in the fiscal year
187
+ */
188
+ export function getDaysRemainingInFiscalYear(date, config = {}) {
189
+ const fiscalYear = getFiscalYear(date, config);
190
+ const fyEnd = getFiscalYearEnd(fiscalYear, config);
191
+ const startOfDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
192
+ const diffMs = fyEnd.getTime() - startOfDay.getTime();
193
+ return Math.ceil(diffMs / (1000 * 60 * 60 * 24));
194
+ }
195
+ /**
196
+ * Get the number of days elapsed in the fiscal year
197
+ * @param date - The date to check
198
+ * @param config - Fiscal year configuration
199
+ * @returns Number of days elapsed in the fiscal year (including the given date)
200
+ */
201
+ export function getDaysElapsedInFiscalYear(date, config = {}) {
202
+ const fiscalYear = getFiscalYear(date, config);
203
+ const fyStart = getFiscalYearStart(fiscalYear, config);
204
+ const startOfDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
205
+ const diffMs = startOfDay.getTime() - fyStart.getTime();
206
+ return Math.floor(diffMs / (1000 * 60 * 60 * 24)) + 1;
207
+ }
208
+ /**
209
+ * Get fiscal year progress as a percentage
210
+ * @param date - The date to check
211
+ * @param config - Fiscal year configuration
212
+ * @returns Percentage of fiscal year completed (0-100)
213
+ */
214
+ export function getFiscalYearProgress(date, config = {}) {
215
+ const fiscalYear = getFiscalYear(date, config);
216
+ const fyStart = getFiscalYearStart(fiscalYear, config);
217
+ const fyEnd = getFiscalYearEnd(fiscalYear, config);
218
+ const totalDays = Math.ceil((fyEnd.getTime() - fyStart.getTime()) / (1000 * 60 * 60 * 24));
219
+ const elapsed = getDaysElapsedInFiscalYear(date, config);
220
+ return Math.min(100, Math.round((elapsed / totalDays) * 10000) / 100);
221
+ }
222
+ /**
223
+ * Get the fiscal week number (1-53) within the fiscal year
224
+ * @param date - The date to check
225
+ * @param config - Fiscal year configuration
226
+ * @returns The fiscal week number
227
+ */
228
+ export function getFiscalWeek(date, config = {}) {
229
+ const elapsed = getDaysElapsedInFiscalYear(date, config);
230
+ return Math.ceil(elapsed / 7);
231
+ }
232
+ /**
233
+ * Common fiscal year configurations
234
+ */
235
+ export const FISCAL_PRESETS = {
236
+ /** Calendar year (January start) - Default */
237
+ CALENDAR: { startMonth: 1 },
238
+ /** UK/India government fiscal year (April start) */
239
+ UK_INDIA: { startMonth: 4 },
240
+ /** Australian fiscal year (July start) */
241
+ AUSTRALIA: { startMonth: 7 },
242
+ /** US federal government fiscal year (October start) */
243
+ US_FEDERAL: { startMonth: 10 },
244
+ };
245
+ /**
246
+ * Format fiscal year as string (e.g., "FY2024" or "FY2023/24")
247
+ * @param fiscalYear - The fiscal year
248
+ * @param config - Fiscal year configuration
249
+ * @param format - Format style: 'short' (FY2024) or 'long' (FY2023/24)
250
+ * @returns Formatted fiscal year string
251
+ */
252
+ export function formatFiscalYear(fiscalYear, config = {}, format = 'short') {
253
+ const { startMonth } = { ...DEFAULT_CONFIG, ...config };
254
+ if (format === 'short' || startMonth === 1) {
255
+ return `FY${fiscalYear}`;
256
+ }
257
+ // For non-calendar fiscal years, show both years
258
+ const calendarYearStart = fiscalYear - 1;
259
+ const shortEndYear = fiscalYear.toString().slice(-2);
260
+ return `FY${calendarYearStart}/${shortEndYear}`;
261
+ }
262
+ /**
263
+ * Format fiscal quarter as string (e.g., "Q1 FY2024")
264
+ * @param fiscalYear - The fiscal year
265
+ * @param quarter - The quarter (1-4)
266
+ * @param config - Fiscal year configuration
267
+ * @returns Formatted fiscal quarter string
268
+ */
269
+ export function formatFiscalQuarter(fiscalYear, quarter, config = {}) {
270
+ const fyString = formatFiscalYear(fiscalYear, config, 'short');
271
+ return `Q${quarter} ${fyString}`;
272
+ }
273
+ /**
274
+ * Get fiscal period info for a date
275
+ * @param date - The date to analyze
276
+ * @param config - Fiscal year configuration
277
+ * @returns Object with fiscal period information
278
+ */
279
+ export function getFiscalPeriodInfo(date, config = {}) {
280
+ const fiscalYear = getFiscalYear(date, config);
281
+ const fiscalQuarter = getFiscalQuarter(date, config);
282
+ return {
283
+ fiscalYear,
284
+ fiscalQuarter,
285
+ fiscalMonth: getFiscalMonth(date, config),
286
+ fiscalWeek: getFiscalWeek(date, config),
287
+ daysElapsed: getDaysElapsedInFiscalYear(date, config),
288
+ daysRemaining: getDaysRemainingInFiscalYear(date, config),
289
+ progress: getFiscalYearProgress(date, config),
290
+ quarterStart: getFiscalQuarterStart(fiscalYear, fiscalQuarter, config),
291
+ quarterEnd: getFiscalQuarterEnd(fiscalYear, fiscalQuarter, config),
292
+ yearStart: getFiscalYearStart(fiscalYear, config),
293
+ yearEnd: getFiscalYearEnd(fiscalYear, config),
294
+ };
295
+ }
package/dist/format.d.ts CHANGED
@@ -22,4 +22,69 @@ export declare function formatTime(date: Date, format?: '12h' | '24h' | 'iso'):
22
22
  * @param duration - duration string (e.g., "1h 30m", "2d", "45s")
23
23
  */
24
24
  export declare function parseDuration(duration: string): number;
25
+ /**
26
+ * Format a date using a pattern string
27
+ * @param date - date to format
28
+ * @param pattern - format pattern (e.g., "YYYY-MM-DD", "MMM D, YYYY")
29
+ *
30
+ * Supported tokens:
31
+ * - YYYY: 4-digit year
32
+ * - YY: 2-digit year
33
+ * - MMMM: Full month name (January)
34
+ * - MMM: Short month name (Jan)
35
+ * - MM: Month with leading zero (01-12)
36
+ * - M: Month (1-12)
37
+ * - DDDD: Full day name (Monday)
38
+ * - DDD: Short day name (Mon)
39
+ * - DD: Day with leading zero (01-31)
40
+ * - D: Day (1-31)
41
+ * - HH: Hours 24h with leading zero (00-23)
42
+ * - H: Hours 24h (0-23)
43
+ * - hh: Hours 12h with leading zero (01-12)
44
+ * - h: Hours 12h (1-12)
45
+ * - mm: Minutes with leading zero (00-59)
46
+ * - ss: Seconds with leading zero (00-59)
47
+ * - SSS: Milliseconds (000-999)
48
+ * - A: AM/PM
49
+ * - a: am/pm
50
+ */
51
+ export declare function formatDate(date: Date, pattern: string): string;
52
+ /**
53
+ * Format a relative time string (e.g., "in 2 days", "3 hours ago")
54
+ * @param date - target date
55
+ * @param baseDate - base date (default: now)
56
+ */
57
+ export declare function formatRelativeTime(date: Date, baseDate?: Date): string;
58
+ /**
59
+ * Format a date range (e.g., "Jan 5 - Jan 10, 2025")
60
+ * @param start - start date
61
+ * @param end - end date
62
+ * @param options - formatting options
63
+ */
64
+ export declare function formatDateRange(start: Date, end: Date, options?: {
65
+ separator?: string;
66
+ includeTime?: boolean;
67
+ format?: 'short' | 'medium' | 'long';
68
+ }): string;
69
+ /**
70
+ * Format a number as an ordinal (1st, 2nd, 3rd, etc.)
71
+ * @param n - number to format
72
+ */
73
+ export declare function formatOrdinal(n: number): string;
74
+ /**
75
+ * Format a day of month as an ordinal (1st, 2nd, 3rd, etc.)
76
+ * @param date - date to format
77
+ */
78
+ export declare function formatDayOrdinal(date: Date): string;
79
+ /**
80
+ * Format a duration in a compact form (e.g., "02:30:45")
81
+ * @param ms - milliseconds
82
+ * @param showHours - always show hours even if 0
83
+ */
84
+ export declare function formatDurationCompact(ms: number, showHours?: boolean): string;
85
+ /**
86
+ * Format a date/time for display in a calendar
87
+ * @param date - date to format
88
+ */
89
+ export declare function formatCalendarDate(date: Date): string;
25
90
  //# sourceMappingURL=format.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,aAAa,EACd,MAAM,gBAAgB,CAAC;AAGxB;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CA2E9E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CA+CvE;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAE,KAAK,GAAG,KAAK,GAAG,KAAa,GAAG,MAAM,CAcpF;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAyDtD"}
1
+ {"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,aAAa,EACd,MAAM,gBAAgB,CAAC;AAGxB;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CA2E9E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CA+CvE;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAA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