ts-time-utils 0.0.1

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 (80) hide show
  1. package/README.md +343 -0
  2. package/dist/age.d.ts +49 -0
  3. package/dist/age.d.ts.map +1 -0
  4. package/dist/age.js +106 -0
  5. package/dist/calculate.d.ts +49 -0
  6. package/dist/calculate.d.ts.map +1 -0
  7. package/dist/calculate.js +179 -0
  8. package/dist/calendar.d.ts +82 -0
  9. package/dist/calendar.d.ts.map +1 -0
  10. package/dist/calendar.js +154 -0
  11. package/dist/constants.d.ts +35 -0
  12. package/dist/constants.d.ts.map +1 -0
  13. package/dist/constants.js +17 -0
  14. package/dist/esm/age.d.ts +49 -0
  15. package/dist/esm/age.d.ts.map +1 -0
  16. package/dist/esm/age.js +106 -0
  17. package/dist/esm/calculate.d.ts +49 -0
  18. package/dist/esm/calculate.d.ts.map +1 -0
  19. package/dist/esm/calculate.js +179 -0
  20. package/dist/esm/calendar.d.ts +82 -0
  21. package/dist/esm/calendar.d.ts.map +1 -0
  22. package/dist/esm/calendar.js +154 -0
  23. package/dist/esm/constants.d.ts +35 -0
  24. package/dist/esm/constants.d.ts.map +1 -0
  25. package/dist/esm/constants.js +17 -0
  26. package/dist/esm/format.d.ts +25 -0
  27. package/dist/esm/format.d.ts.map +1 -0
  28. package/dist/esm/format.js +189 -0
  29. package/dist/esm/index.d.ts +17 -0
  30. package/dist/esm/index.d.ts.map +1 -0
  31. package/dist/esm/index.js +28 -0
  32. package/dist/esm/interval.d.ts +30 -0
  33. package/dist/esm/interval.d.ts.map +1 -0
  34. package/dist/esm/interval.js +86 -0
  35. package/dist/esm/parse.d.ts +31 -0
  36. package/dist/esm/parse.d.ts.map +1 -0
  37. package/dist/esm/parse.js +217 -0
  38. package/dist/esm/performance.d.ts +110 -0
  39. package/dist/esm/performance.d.ts.map +1 -0
  40. package/dist/esm/performance.js +222 -0
  41. package/dist/esm/rangePresets.d.ts +45 -0
  42. package/dist/esm/rangePresets.d.ts.map +1 -0
  43. package/dist/esm/rangePresets.js +124 -0
  44. package/dist/esm/timezone.d.ts +38 -0
  45. package/dist/esm/timezone.d.ts.map +1 -0
  46. package/dist/esm/timezone.js +99 -0
  47. package/dist/esm/validate.d.ts +62 -0
  48. package/dist/esm/validate.d.ts.map +1 -0
  49. package/dist/esm/validate.js +108 -0
  50. package/dist/esm/workingHours.d.ts +25 -0
  51. package/dist/esm/workingHours.d.ts.map +1 -0
  52. package/dist/esm/workingHours.js +107 -0
  53. package/dist/format.d.ts +25 -0
  54. package/dist/format.d.ts.map +1 -0
  55. package/dist/format.js +189 -0
  56. package/dist/index.d.ts +17 -0
  57. package/dist/index.d.ts.map +1 -0
  58. package/dist/index.js +28 -0
  59. package/dist/interval.d.ts +30 -0
  60. package/dist/interval.d.ts.map +1 -0
  61. package/dist/interval.js +86 -0
  62. package/dist/parse.d.ts +31 -0
  63. package/dist/parse.d.ts.map +1 -0
  64. package/dist/parse.js +217 -0
  65. package/dist/performance.d.ts +110 -0
  66. package/dist/performance.d.ts.map +1 -0
  67. package/dist/performance.js +222 -0
  68. package/dist/rangePresets.d.ts +45 -0
  69. package/dist/rangePresets.d.ts.map +1 -0
  70. package/dist/rangePresets.js +124 -0
  71. package/dist/timezone.d.ts +38 -0
  72. package/dist/timezone.d.ts.map +1 -0
  73. package/dist/timezone.js +99 -0
  74. package/dist/validate.d.ts +62 -0
  75. package/dist/validate.d.ts.map +1 -0
  76. package/dist/validate.js +108 -0
  77. package/dist/workingHours.d.ts +25 -0
  78. package/dist/workingHours.d.ts.map +1 -0
  79. package/dist/workingHours.js +107 -0
  80. package/package.json +102 -0
@@ -0,0 +1,179 @@
1
+ import { MILLISECONDS_PER_SECOND, MILLISECONDS_PER_MINUTE, MILLISECONDS_PER_HOUR, MILLISECONDS_PER_DAY, MILLISECONDS_PER_WEEK, MILLISECONDS_PER_MONTH, MILLISECONDS_PER_YEAR } from './constants.js';
2
+ /**
3
+ * Calculate difference between two dates in specified unit
4
+ * @param date1 - first date
5
+ * @param date2 - second date
6
+ * @param unit - unit to return the difference in
7
+ * @param precise - if true, returns decimal values; if false, returns integers
8
+ */
9
+ export function differenceInUnits(date1, date2, unit = 'milliseconds', precise = true) {
10
+ const diffMs = Math.abs(date1.getTime() - date2.getTime());
11
+ let result;
12
+ switch (unit) {
13
+ case 'years':
14
+ result = diffMs / MILLISECONDS_PER_YEAR;
15
+ break;
16
+ case 'months':
17
+ result = diffMs / MILLISECONDS_PER_MONTH;
18
+ break;
19
+ case 'weeks':
20
+ result = diffMs / MILLISECONDS_PER_WEEK;
21
+ break;
22
+ case 'days':
23
+ result = diffMs / MILLISECONDS_PER_DAY;
24
+ break;
25
+ case 'hours':
26
+ result = diffMs / MILLISECONDS_PER_HOUR;
27
+ break;
28
+ case 'minutes':
29
+ result = diffMs / MILLISECONDS_PER_MINUTE;
30
+ break;
31
+ case 'seconds':
32
+ result = diffMs / MILLISECONDS_PER_SECOND;
33
+ break;
34
+ case 'milliseconds':
35
+ default:
36
+ result = diffMs;
37
+ break;
38
+ }
39
+ return precise ? result : Math.floor(result);
40
+ }
41
+ /**
42
+ * Add time to a date
43
+ * @param date - base date
44
+ * @param amount - amount to add
45
+ * @param unit - unit of the amount
46
+ */
47
+ export function addTime(date, amount, unit) {
48
+ const result = new Date(date);
49
+ let milliseconds;
50
+ switch (unit) {
51
+ case 'years':
52
+ milliseconds = amount * MILLISECONDS_PER_YEAR;
53
+ break;
54
+ case 'months':
55
+ milliseconds = amount * MILLISECONDS_PER_MONTH;
56
+ break;
57
+ case 'weeks':
58
+ milliseconds = amount * MILLISECONDS_PER_WEEK;
59
+ break;
60
+ case 'days':
61
+ milliseconds = amount * MILLISECONDS_PER_DAY;
62
+ break;
63
+ case 'hours':
64
+ milliseconds = amount * MILLISECONDS_PER_HOUR;
65
+ break;
66
+ case 'minutes':
67
+ milliseconds = amount * MILLISECONDS_PER_MINUTE;
68
+ break;
69
+ case 'seconds':
70
+ milliseconds = amount * MILLISECONDS_PER_SECOND;
71
+ break;
72
+ case 'milliseconds':
73
+ default:
74
+ milliseconds = amount;
75
+ break;
76
+ }
77
+ result.setTime(result.getTime() + milliseconds);
78
+ return result;
79
+ }
80
+ /**
81
+ * Subtract time from a date
82
+ * @param date - base date
83
+ * @param amount - amount to subtract
84
+ * @param unit - unit of the amount
85
+ */
86
+ export function subtractTime(date, amount, unit) {
87
+ return addTime(date, -amount, unit);
88
+ }
89
+ /**
90
+ * Get the start of a time period for a given date
91
+ * @param date - input date
92
+ * @param unit - time unit to get the start of
93
+ */
94
+ export function startOf(date, unit) {
95
+ const result = new Date(date);
96
+ switch (unit) {
97
+ case 'minute':
98
+ result.setSeconds(0, 0);
99
+ break;
100
+ case 'hour':
101
+ result.setMinutes(0, 0, 0);
102
+ break;
103
+ case 'day':
104
+ result.setHours(0, 0, 0, 0);
105
+ break;
106
+ case 'week':
107
+ result.setHours(0, 0, 0, 0);
108
+ result.setDate(result.getDate() - result.getDay());
109
+ break;
110
+ case 'month':
111
+ result.setHours(0, 0, 0, 0);
112
+ result.setDate(1);
113
+ break;
114
+ case 'year':
115
+ result.setHours(0, 0, 0, 0);
116
+ result.setMonth(0, 1);
117
+ break;
118
+ }
119
+ return result;
120
+ }
121
+ /**
122
+ * Get the end of a time period for a given date
123
+ * @param date - input date
124
+ * @param unit - time unit to get the end of
125
+ */
126
+ export function endOf(date, unit) {
127
+ const result = new Date(date);
128
+ switch (unit) {
129
+ case 'minute':
130
+ result.setSeconds(59, 999);
131
+ break;
132
+ case 'hour':
133
+ result.setMinutes(59, 59, 999);
134
+ break;
135
+ case 'day':
136
+ result.setHours(23, 59, 59, 999);
137
+ break;
138
+ case 'week':
139
+ result.setHours(23, 59, 59, 999);
140
+ result.setDate(result.getDate() + (6 - result.getDay()));
141
+ break;
142
+ case 'month':
143
+ result.setHours(23, 59, 59, 999);
144
+ result.setMonth(result.getMonth() + 1, 0);
145
+ break;
146
+ case 'year':
147
+ result.setHours(23, 59, 59, 999);
148
+ result.setMonth(11, 31);
149
+ break;
150
+ }
151
+ return result;
152
+ }
153
+ /**
154
+ * Check if a date is between two other dates
155
+ * @param date - date to check
156
+ * @param start - start date (inclusive)
157
+ * @param end - end date (inclusive)
158
+ */
159
+ export function isBetween(date, start, end) {
160
+ const time = date.getTime();
161
+ return time >= start.getTime() && time <= end.getTime();
162
+ }
163
+ /**
164
+ * Get the number of business days between two dates (excludes weekends)
165
+ * @param startDate - start date
166
+ * @param endDate - end date
167
+ */
168
+ export function businessDaysBetween(startDate, endDate) {
169
+ let count = 0;
170
+ const current = new Date(startDate);
171
+ while (current <= endDate) {
172
+ const dayOfWeek = current.getDay();
173
+ if (dayOfWeek !== 0 && dayOfWeek !== 6) { // Not Sunday (0) or Saturday (6)
174
+ count++;
175
+ }
176
+ current.setDate(current.getDate() + 1);
177
+ }
178
+ return count;
179
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Calendar and holiday utilities
3
+ */
4
+ /**
5
+ * Get the week number of the year (ISO 8601)
6
+ * @param date - date to get week number for
7
+ */
8
+ export declare function getWeekNumber(date: Date): number;
9
+ /**
10
+ * Get the week of the month (1-6)
11
+ * @param date - date to get week of month for
12
+ */
13
+ export declare function getWeekOfMonth(date: Date): number;
14
+ /**
15
+ * Get the quarter of the year (1-4)
16
+ * @param date - date to get quarter for
17
+ */
18
+ export declare function getQuarter(date: Date): number;
19
+ /**
20
+ * Get the day of the year (1-366)
21
+ * @param date - date to get day of year for
22
+ */
23
+ export declare function getDayOfYear(date: Date): number;
24
+ /**
25
+ * Get the number of weeks in a year
26
+ * @param year - year to check
27
+ */
28
+ export declare function getWeeksInYear(year: number): number;
29
+ /**
30
+ * Get the number of days in a month
31
+ * @param year - year
32
+ * @param month - month (0-11)
33
+ */
34
+ export declare function getDaysInMonth(year: number, month: number): number;
35
+ /**
36
+ * Get the number of days in a year
37
+ * @param year - year to check
38
+ */
39
+ export declare function getDaysInYear(year: number): number;
40
+ /**
41
+ * Calculate Easter Sunday for a given year (Western/Gregorian calendar)
42
+ * @param year - year to calculate Easter for
43
+ */
44
+ export declare function getEaster(year: number): Date;
45
+ /**
46
+ * Get all months in a year
47
+ * @param year - year to get months for
48
+ */
49
+ export declare function getMonthsInYear(year: number): Date[];
50
+ /**
51
+ * Get all days in a month
52
+ * @param year - year
53
+ * @param month - month (0-11)
54
+ */
55
+ export declare function getDaysInMonthArray(year: number, month: number): Date[];
56
+ /**
57
+ * Get all weekdays in a month
58
+ * @param year - year
59
+ * @param month - month (0-11)
60
+ */
61
+ export declare function getWeekdaysInMonth(year: number, month: number): Date[];
62
+ /**
63
+ * Get the first day of the month
64
+ * @param date - any date in the month
65
+ */
66
+ export declare function getFirstDayOfMonth(date: Date): Date;
67
+ /**
68
+ * Get the last day of the month
69
+ * @param date - any date in the month
70
+ */
71
+ export declare function getLastDayOfMonth(date: Date): Date;
72
+ /**
73
+ * Get the first day of the year
74
+ * @param year - year
75
+ */
76
+ export declare function getFirstDayOfYear(year: number): Date;
77
+ /**
78
+ * Get the last day of the year
79
+ * @param year - year
80
+ */
81
+ export declare function getLastDayOfYear(year: number): Date;
82
+ //# sourceMappingURL=calendar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calendar.d.ts","sourceRoot":"","sources":["../src/calendar.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAMhD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAKjD;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE7C;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAI/C;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAYnD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAElE;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAUD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAkB5C;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,CAEpD;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,CAGvE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,EAAE,CAKtE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAEnD;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAElD;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEpD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEnD"}
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Calendar and holiday utilities
3
+ */
4
+ /**
5
+ * Get the week number of the year (ISO 8601)
6
+ * @param date - date to get week number for
7
+ */
8
+ export function getWeekNumber(date) {
9
+ const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
10
+ const dayNum = d.getUTCDay() || 7;
11
+ d.setUTCDate(d.getUTCDate() + 4 - dayNum);
12
+ const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
13
+ return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
14
+ }
15
+ /**
16
+ * Get the week of the month (1-6)
17
+ * @param date - date to get week of month for
18
+ */
19
+ export function getWeekOfMonth(date) {
20
+ const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
21
+ const firstWeekday = firstDay.getDay();
22
+ const offsetDate = date.getDate() + firstWeekday - 1;
23
+ return Math.floor(offsetDate / 7) + 1;
24
+ }
25
+ /**
26
+ * Get the quarter of the year (1-4)
27
+ * @param date - date to get quarter for
28
+ */
29
+ export function getQuarter(date) {
30
+ return Math.floor(date.getMonth() / 3) + 1;
31
+ }
32
+ /**
33
+ * Get the day of the year (1-366)
34
+ * @param date - date to get day of year for
35
+ */
36
+ export function getDayOfYear(date) {
37
+ const start = new Date(date.getFullYear(), 0, 0);
38
+ const diff = date.getTime() - start.getTime();
39
+ return Math.floor(diff / (1000 * 60 * 60 * 24));
40
+ }
41
+ /**
42
+ * Get the number of weeks in a year
43
+ * @param year - year to check
44
+ */
45
+ export function getWeeksInYear(year) {
46
+ const jan1 = new Date(year, 0, 1);
47
+ const dec31 = new Date(year, 11, 31);
48
+ // If Jan 1 is Thu-Sun or Dec 31 is Mon-Wed, there are 53 weeks
49
+ const jan1Day = jan1.getDay();
50
+ const dec31Day = dec31.getDay();
51
+ if (jan1Day === 4 || (jan1Day === 3 && dec31Day === 4)) {
52
+ return 53;
53
+ }
54
+ return 52;
55
+ }
56
+ /**
57
+ * Get the number of days in a month
58
+ * @param year - year
59
+ * @param month - month (0-11)
60
+ */
61
+ export function getDaysInMonth(year, month) {
62
+ return new Date(year, month + 1, 0).getDate();
63
+ }
64
+ /**
65
+ * Get the number of days in a year
66
+ * @param year - year to check
67
+ */
68
+ export function getDaysInYear(year) {
69
+ return isLeapYear(year) ? 366 : 365;
70
+ }
71
+ /**
72
+ * Check if a year is a leap year
73
+ * @param year - year to check
74
+ */
75
+ function isLeapYear(year) {
76
+ return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
77
+ }
78
+ /**
79
+ * Calculate Easter Sunday for a given year (Western/Gregorian calendar)
80
+ * @param year - year to calculate Easter for
81
+ */
82
+ export function getEaster(year) {
83
+ // Anonymous Gregorian algorithm
84
+ const a = year % 19;
85
+ const b = Math.floor(year / 100);
86
+ const c = year % 100;
87
+ const d = Math.floor(b / 4);
88
+ const e = b % 4;
89
+ const f = Math.floor((b + 8) / 25);
90
+ const g = Math.floor((b - f + 1) / 3);
91
+ const h = (19 * a + b - d - g + 15) % 30;
92
+ const i = Math.floor(c / 4);
93
+ const k = c % 4;
94
+ const l = (32 + 2 * e + 2 * i - h - k) % 7;
95
+ const m = Math.floor((a + 11 * h + 22 * l) / 451);
96
+ const month = Math.floor((h + l - 7 * m + 114) / 31);
97
+ const day = ((h + l - 7 * m + 114) % 31) + 1;
98
+ return new Date(year, month - 1, day);
99
+ }
100
+ /**
101
+ * Get all months in a year
102
+ * @param year - year to get months for
103
+ */
104
+ export function getMonthsInYear(year) {
105
+ return Array.from({ length: 12 }, (_, i) => new Date(year, i, 1));
106
+ }
107
+ /**
108
+ * Get all days in a month
109
+ * @param year - year
110
+ * @param month - month (0-11)
111
+ */
112
+ export function getDaysInMonthArray(year, month) {
113
+ const daysCount = getDaysInMonth(year, month);
114
+ return Array.from({ length: daysCount }, (_, i) => new Date(year, month, i + 1));
115
+ }
116
+ /**
117
+ * Get all weekdays in a month
118
+ * @param year - year
119
+ * @param month - month (0-11)
120
+ */
121
+ export function getWeekdaysInMonth(year, month) {
122
+ return getDaysInMonthArray(year, month).filter(date => {
123
+ const day = date.getDay();
124
+ return day !== 0 && day !== 6; // Not Sunday or Saturday
125
+ });
126
+ }
127
+ /**
128
+ * Get the first day of the month
129
+ * @param date - any date in the month
130
+ */
131
+ export function getFirstDayOfMonth(date) {
132
+ return new Date(date.getFullYear(), date.getMonth(), 1);
133
+ }
134
+ /**
135
+ * Get the last day of the month
136
+ * @param date - any date in the month
137
+ */
138
+ export function getLastDayOfMonth(date) {
139
+ return new Date(date.getFullYear(), date.getMonth() + 1, 0);
140
+ }
141
+ /**
142
+ * Get the first day of the year
143
+ * @param year - year
144
+ */
145
+ export function getFirstDayOfYear(year) {
146
+ return new Date(year, 0, 1);
147
+ }
148
+ /**
149
+ * Get the last day of the year
150
+ * @param year - year
151
+ */
152
+ export function getLastDayOfYear(year) {
153
+ return new Date(year, 11, 31);
154
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Time constants for millisecond conversions
3
+ */
4
+ export declare const MILLISECONDS_PER_SECOND = 1000;
5
+ export declare const MILLISECONDS_PER_MINUTE: number;
6
+ export declare const MILLISECONDS_PER_HOUR: number;
7
+ export declare const MILLISECONDS_PER_DAY: number;
8
+ export declare const MILLISECONDS_PER_WEEK: number;
9
+ export declare const MILLISECONDS_PER_MONTH: number;
10
+ export declare const MILLISECONDS_PER_YEAR: number;
11
+ /**
12
+ * Second-based constants
13
+ */
14
+ export declare const SECONDS_PER_MINUTE = 60;
15
+ export declare const SECONDS_PER_HOUR: number;
16
+ export declare const SECONDS_PER_DAY: number;
17
+ export declare const SECONDS_PER_WEEK: number;
18
+ /**
19
+ * Common time units
20
+ */
21
+ export type TimeUnit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years';
22
+ /**
23
+ * Formatting options
24
+ */
25
+ export interface FormatOptions {
26
+ /** Include milliseconds in output */
27
+ includeMs?: boolean;
28
+ /** Use short format (e.g., "1h" vs "1 hour") */
29
+ short?: boolean;
30
+ /** Maximum number of units to show */
31
+ maxUnits?: number;
32
+ /** Round to nearest unit instead of floor */
33
+ round?: boolean;
34
+ }
35
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAC5C,eAAO,MAAM,uBAAuB,QAA+B,CAAC;AACpE,eAAO,MAAM,qBAAqB,QAA+B,CAAC;AAClE,eAAO,MAAM,oBAAoB,QAA6B,CAAC;AAC/D,eAAO,MAAM,qBAAqB,QAA2B,CAAC;AAC9D,eAAO,MAAM,sBAAsB,QAA4B,CAAC;AAChE,eAAO,MAAM,qBAAqB,QAA6B,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,kBAAkB,KAAK,CAAC;AACrC,eAAO,MAAM,gBAAgB,QAA0B,CAAC;AACxD,eAAO,MAAM,eAAe,QAAwB,CAAC;AACrD,eAAO,MAAM,gBAAgB,QAAsB,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,cAAc,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEhH;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,qCAAqC;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,gDAAgD;IAChD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,sCAAsC;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6CAA6C;IAC7C,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Time constants for millisecond conversions
3
+ */
4
+ export const MILLISECONDS_PER_SECOND = 1000;
5
+ export const MILLISECONDS_PER_MINUTE = 60 * MILLISECONDS_PER_SECOND;
6
+ export const MILLISECONDS_PER_HOUR = 60 * MILLISECONDS_PER_MINUTE;
7
+ export const MILLISECONDS_PER_DAY = 24 * MILLISECONDS_PER_HOUR;
8
+ export const MILLISECONDS_PER_WEEK = 7 * MILLISECONDS_PER_DAY;
9
+ export const MILLISECONDS_PER_MONTH = 30 * MILLISECONDS_PER_DAY; // Approximate
10
+ export const MILLISECONDS_PER_YEAR = 365 * MILLISECONDS_PER_DAY; // Approximate
11
+ /**
12
+ * Second-based constants
13
+ */
14
+ export const SECONDS_PER_MINUTE = 60;
15
+ export const SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE;
16
+ export const SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
17
+ export const SECONDS_PER_WEEK = 7 * SECONDS_PER_DAY;
@@ -0,0 +1,49 @@
1
+ import { TimeUnit } from './constants.js';
2
+ /**
3
+ * Age calculation result
4
+ */
5
+ export interface AgeResult {
6
+ years: number;
7
+ months: number;
8
+ days: number;
9
+ totalDays: number;
10
+ totalMonths: number;
11
+ }
12
+ /**
13
+ * Calculate detailed age from birth date
14
+ * @param birthDate - date of birth
15
+ * @param referenceDate - date to calculate age from (defaults to now)
16
+ */
17
+ export declare function calculateAge(birthDate: Date, referenceDate?: Date): AgeResult;
18
+ /**
19
+ * Get age in specific units
20
+ * @param birthDate - date of birth
21
+ * @param unit - unit to return age in
22
+ * @param referenceDate - date to calculate from (defaults to now)
23
+ */
24
+ export declare function getAgeInUnits(birthDate: Date, unit: TimeUnit, referenceDate?: Date): number;
25
+ /**
26
+ * Determine life stage based on age
27
+ * @param birthDate - date of birth
28
+ * @param referenceDate - date to calculate from (defaults to now)
29
+ */
30
+ export declare function getLifeStage(birthDate: Date, referenceDate?: Date): 'infant' | 'child' | 'teen' | 'adult' | 'senior';
31
+ /**
32
+ * Get the next birthday date
33
+ * @param birthDate - date of birth
34
+ * @param referenceDate - date to calculate from (defaults to now)
35
+ */
36
+ export declare function getNextBirthday(birthDate: Date, referenceDate?: Date): Date;
37
+ /**
38
+ * Get days until next birthday
39
+ * @param birthDate - date of birth
40
+ * @param referenceDate - date to calculate from (defaults to now)
41
+ */
42
+ export declare function getDaysUntilBirthday(birthDate: Date, referenceDate?: Date): number;
43
+ /**
44
+ * Check if today is someone's birthday
45
+ * @param birthDate - date of birth
46
+ * @param referenceDate - date to check (defaults to now)
47
+ */
48
+ export declare function isBirthday(birthDate: Date, referenceDate?: Date): boolean;
49
+ //# sourceMappingURL=age.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"age.d.ts","sourceRoot":"","sources":["../../src/age.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,SAAS,CAyBzF;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,IAAI,EACf,IAAI,EAAE,QAAQ,EACd,aAAa,GAAE,IAAiB,GAC/B,MAAM,CAsBR;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,IAAI,EACf,aAAa,GAAE,IAAiB,GAC/B,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAQlD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,IAAI,CAYvF;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,MAAM,CAG9F;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,OAAO,CAQrF"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Calculate detailed age from birth date
3
+ * @param birthDate - date of birth
4
+ * @param referenceDate - date to calculate age from (defaults to now)
5
+ */
6
+ export function calculateAge(birthDate, referenceDate = new Date()) {
7
+ const birth = new Date(birthDate);
8
+ const reference = new Date(referenceDate);
9
+ let years = reference.getFullYear() - birth.getFullYear();
10
+ let months = reference.getMonth() - birth.getMonth();
11
+ let days = reference.getDate() - birth.getDate();
12
+ // Adjust for negative days
13
+ if (days < 0) {
14
+ months--;
15
+ const lastMonth = new Date(reference.getFullYear(), reference.getMonth(), 0);
16
+ days += lastMonth.getDate();
17
+ }
18
+ // Adjust for negative months
19
+ if (months < 0) {
20
+ years--;
21
+ months += 12;
22
+ }
23
+ const totalDays = Math.floor((reference.getTime() - birth.getTime()) / (1000 * 60 * 60 * 24));
24
+ const totalMonths = years * 12 + months;
25
+ return { years, months, days, totalDays, totalMonths };
26
+ }
27
+ /**
28
+ * Get age in specific units
29
+ * @param birthDate - date of birth
30
+ * @param unit - unit to return age in
31
+ * @param referenceDate - date to calculate from (defaults to now)
32
+ */
33
+ export function getAgeInUnits(birthDate, unit, referenceDate = new Date()) {
34
+ const diffMs = referenceDate.getTime() - birthDate.getTime();
35
+ switch (unit) {
36
+ case 'years':
37
+ return Math.floor(diffMs / (1000 * 60 * 60 * 24 * 365.25));
38
+ case 'months':
39
+ return Math.floor(diffMs / (1000 * 60 * 60 * 24 * 30.44));
40
+ case 'weeks':
41
+ return Math.floor(diffMs / (1000 * 60 * 60 * 24 * 7));
42
+ case 'days':
43
+ return Math.floor(diffMs / (1000 * 60 * 60 * 24));
44
+ case 'hours':
45
+ return Math.floor(diffMs / (1000 * 60 * 60));
46
+ case 'minutes':
47
+ return Math.floor(diffMs / (1000 * 60));
48
+ case 'seconds':
49
+ return Math.floor(diffMs / 1000);
50
+ case 'milliseconds':
51
+ default:
52
+ return diffMs;
53
+ }
54
+ }
55
+ /**
56
+ * Determine life stage based on age
57
+ * @param birthDate - date of birth
58
+ * @param referenceDate - date to calculate from (defaults to now)
59
+ */
60
+ export function getLifeStage(birthDate, referenceDate = new Date()) {
61
+ const ageInYears = getAgeInUnits(birthDate, 'years', referenceDate);
62
+ if (ageInYears < 2)
63
+ return 'infant';
64
+ if (ageInYears < 13)
65
+ return 'child';
66
+ if (ageInYears < 18)
67
+ return 'teen';
68
+ if (ageInYears < 65)
69
+ return 'adult';
70
+ return 'senior';
71
+ }
72
+ /**
73
+ * Get the next birthday date
74
+ * @param birthDate - date of birth
75
+ * @param referenceDate - date to calculate from (defaults to now)
76
+ */
77
+ export function getNextBirthday(birthDate, referenceDate = new Date()) {
78
+ const birth = new Date(birthDate);
79
+ const reference = new Date(referenceDate);
80
+ const nextBirthday = new Date(reference.getFullYear(), birth.getMonth(), birth.getDate());
81
+ // If birthday has passed this year, get next year's birthday
82
+ if (nextBirthday <= reference) {
83
+ nextBirthday.setFullYear(reference.getFullYear() + 1);
84
+ }
85
+ return nextBirthday;
86
+ }
87
+ /**
88
+ * Get days until next birthday
89
+ * @param birthDate - date of birth
90
+ * @param referenceDate - date to calculate from (defaults to now)
91
+ */
92
+ export function getDaysUntilBirthday(birthDate, referenceDate = new Date()) {
93
+ const nextBirthday = getNextBirthday(birthDate, referenceDate);
94
+ return Math.ceil((nextBirthday.getTime() - referenceDate.getTime()) / (1000 * 60 * 60 * 24));
95
+ }
96
+ /**
97
+ * Check if today is someone's birthday
98
+ * @param birthDate - date of birth
99
+ * @param referenceDate - date to check (defaults to now)
100
+ */
101
+ export function isBirthday(birthDate, referenceDate = new Date()) {
102
+ const birth = new Date(birthDate);
103
+ const reference = new Date(referenceDate);
104
+ return (birth.getMonth() === reference.getMonth() &&
105
+ birth.getDate() === reference.getDate());
106
+ }
@@ -0,0 +1,49 @@
1
+ import { TimeUnit } from './constants.js';
2
+ /**
3
+ * Calculate difference between two dates in specified unit
4
+ * @param date1 - first date
5
+ * @param date2 - second date
6
+ * @param unit - unit to return the difference in
7
+ * @param precise - if true, returns decimal values; if false, returns integers
8
+ */
9
+ export declare function differenceInUnits(date1: Date, date2: Date, unit?: TimeUnit, precise?: boolean): number;
10
+ /**
11
+ * Add time to a date
12
+ * @param date - base date
13
+ * @param amount - amount to add
14
+ * @param unit - unit of the amount
15
+ */
16
+ export declare function addTime(date: Date, amount: number, unit: TimeUnit): Date;
17
+ /**
18
+ * Subtract time from a date
19
+ * @param date - base date
20
+ * @param amount - amount to subtract
21
+ * @param unit - unit of the amount
22
+ */
23
+ export declare function subtractTime(date: Date, amount: number, unit: TimeUnit): Date;
24
+ /**
25
+ * Get the start of a time period for a given date
26
+ * @param date - input date
27
+ * @param unit - time unit to get the start of
28
+ */
29
+ export declare function startOf(date: Date, unit: 'day' | 'week' | 'month' | 'year' | 'hour' | 'minute'): Date;
30
+ /**
31
+ * Get the end of a time period for a given date
32
+ * @param date - input date
33
+ * @param unit - time unit to get the end of
34
+ */
35
+ export declare function endOf(date: Date, unit: 'day' | 'week' | 'month' | 'year' | 'hour' | 'minute'): Date;
36
+ /**
37
+ * Check if a date is between two other dates
38
+ * @param date - date to check
39
+ * @param start - start date (inclusive)
40
+ * @param end - end date (inclusive)
41
+ */
42
+ export declare function isBetween(date: Date, start: Date, end: Date): boolean;
43
+ /**
44
+ * Get the number of business days between two dates (excludes weekends)
45
+ * @param startDate - start date
46
+ * @param endDate - end date
47
+ */
48
+ export declare function businessDaysBetween(startDate: Date, endDate: Date): number;
49
+ //# sourceMappingURL=calculate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calculate.d.ts","sourceRoot":"","sources":["../../src/calculate.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,QAAQ,EACT,MAAM,gBAAgB,CAAC;AAExB;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,IAAI,EACX,KAAK,EAAE,IAAI,EACX,IAAI,GAAE,QAAyB,EAC/B,OAAO,GAAE,OAAc,GACtB,MAAM,CAkCR;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAmCxE;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAE7E;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BrG;AAED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CA4BnG;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,GAAG,OAAO,CAGrE;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,GAAG,MAAM,CAa1E"}