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.
- package/README.md +343 -0
- package/dist/age.d.ts +49 -0
- package/dist/age.d.ts.map +1 -0
- package/dist/age.js +106 -0
- package/dist/calculate.d.ts +49 -0
- package/dist/calculate.d.ts.map +1 -0
- package/dist/calculate.js +179 -0
- package/dist/calendar.d.ts +82 -0
- package/dist/calendar.d.ts.map +1 -0
- package/dist/calendar.js +154 -0
- package/dist/constants.d.ts +35 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +17 -0
- package/dist/esm/age.d.ts +49 -0
- package/dist/esm/age.d.ts.map +1 -0
- package/dist/esm/age.js +106 -0
- package/dist/esm/calculate.d.ts +49 -0
- package/dist/esm/calculate.d.ts.map +1 -0
- package/dist/esm/calculate.js +179 -0
- package/dist/esm/calendar.d.ts +82 -0
- package/dist/esm/calendar.d.ts.map +1 -0
- package/dist/esm/calendar.js +154 -0
- package/dist/esm/constants.d.ts +35 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +17 -0
- package/dist/esm/format.d.ts +25 -0
- package/dist/esm/format.d.ts.map +1 -0
- package/dist/esm/format.js +189 -0
- package/dist/esm/index.d.ts +17 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +28 -0
- package/dist/esm/interval.d.ts +30 -0
- package/dist/esm/interval.d.ts.map +1 -0
- package/dist/esm/interval.js +86 -0
- package/dist/esm/parse.d.ts +31 -0
- package/dist/esm/parse.d.ts.map +1 -0
- package/dist/esm/parse.js +217 -0
- package/dist/esm/performance.d.ts +110 -0
- package/dist/esm/performance.d.ts.map +1 -0
- package/dist/esm/performance.js +222 -0
- package/dist/esm/rangePresets.d.ts +45 -0
- package/dist/esm/rangePresets.d.ts.map +1 -0
- package/dist/esm/rangePresets.js +124 -0
- package/dist/esm/timezone.d.ts +38 -0
- package/dist/esm/timezone.d.ts.map +1 -0
- package/dist/esm/timezone.js +99 -0
- package/dist/esm/validate.d.ts +62 -0
- package/dist/esm/validate.d.ts.map +1 -0
- package/dist/esm/validate.js +108 -0
- package/dist/esm/workingHours.d.ts +25 -0
- package/dist/esm/workingHours.d.ts.map +1 -0
- package/dist/esm/workingHours.js +107 -0
- package/dist/format.d.ts +25 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/format.js +189 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/interval.d.ts +30 -0
- package/dist/interval.d.ts.map +1 -0
- package/dist/interval.js +86 -0
- package/dist/parse.d.ts +31 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +217 -0
- package/dist/performance.d.ts +110 -0
- package/dist/performance.d.ts.map +1 -0
- package/dist/performance.js +222 -0
- package/dist/rangePresets.d.ts +45 -0
- package/dist/rangePresets.d.ts.map +1 -0
- package/dist/rangePresets.js +124 -0
- package/dist/timezone.d.ts +38 -0
- package/dist/timezone.d.ts.map +1 -0
- package/dist/timezone.js +99 -0
- package/dist/validate.d.ts +62 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +108 -0
- package/dist/workingHours.d.ts +25 -0
- package/dist/workingHours.d.ts.map +1 -0
- package/dist/workingHours.js +107 -0
- package/package.json +102 -0
package/README.md
ADDED
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
# ts-time-utils
|
|
2
|
+
|
|
3
|
+
A lightweight TypeScript utility library for time formatting, calculations, and validation with full tree-shaking support.
|
|
4
|
+
|
|
5
|
+
## 🚀 Features
|
|
6
|
+
|
|
7
|
+
- **📦 Lightweight** - Import only what you need with tree-shaking support
|
|
8
|
+
- **⚡ Fast** - Zero dependencies, pure JavaScript functions
|
|
9
|
+
- **🔧 TypeScript** - Full type safety and IntelliSense support
|
|
10
|
+
- **🌳 Tree-shakable** - Import individual functions to minimize bundle size
|
|
11
|
+
- **📚 Comprehensive** - 12 utility categories with 70+ functions
|
|
12
|
+
|
|
13
|
+
### 🎨 Format utilities
|
|
14
|
+
|
|
15
|
+
- Format milliseconds to human-readable durations
|
|
16
|
+
- Get human-friendly "time ago" strings
|
|
17
|
+
- Parse duration strings back to milliseconds
|
|
18
|
+
- Format time in 12h/24h/ISO formats
|
|
19
|
+
|
|
20
|
+
### 🧮 Calculation utilities
|
|
21
|
+
|
|
22
|
+
- Calculate difference between dates in any unit
|
|
23
|
+
- Add/subtract time from dates
|
|
24
|
+
- Get start/end of time periods
|
|
25
|
+
- Business days calculations
|
|
26
|
+
- Check if date is between two dates
|
|
27
|
+
|
|
28
|
+
### ✅ Validation utilities
|
|
29
|
+
|
|
30
|
+
- Validate dates and time strings
|
|
31
|
+
- Check for leap years, weekends, past/future dates
|
|
32
|
+
- Compare dates (same day, today, yesterday, etc.)
|
|
33
|
+
|
|
34
|
+
### 🎂 Age utilities
|
|
35
|
+
|
|
36
|
+
- Calculate precise age with years, months, and days
|
|
37
|
+
- Get life stage classifications (infant, child, adult, etc.)
|
|
38
|
+
- Birthday calculations and next birthday finder
|
|
39
|
+
- Check if today is someone's birthday
|
|
40
|
+
|
|
41
|
+
### 📅 Calendar utilities
|
|
42
|
+
|
|
43
|
+
- ISO week numbers and week-based calculations
|
|
44
|
+
- Quarter operations and fiscal year support
|
|
45
|
+
- Holiday calculations (Easter, Thanksgiving, etc.)
|
|
46
|
+
- Days in month/year calculations
|
|
47
|
+
|
|
48
|
+
### 🔍 Parse utilities
|
|
49
|
+
|
|
50
|
+
- Advanced date parsing from multiple formats
|
|
51
|
+
- Relative date parsing ("tomorrow", "next week")
|
|
52
|
+
- Custom format parsing with flexible patterns
|
|
53
|
+
- Smart date interpretation
|
|
54
|
+
|
|
55
|
+
### ⚡ Performance utilities
|
|
56
|
+
|
|
57
|
+
- Async utilities (sleep, timeout, retry)
|
|
58
|
+
- Performance measurement and benchmarking
|
|
59
|
+
- Stopwatch for timing operations
|
|
60
|
+
- Function utilities (debounce, throttle, memoize)
|
|
61
|
+
|
|
62
|
+
### 📏 Interval utilities
|
|
63
|
+
|
|
64
|
+
- Create and validate intervals
|
|
65
|
+
- Overlap, intersection, merge, subtraction
|
|
66
|
+
- Split by day and total coverage
|
|
67
|
+
- Normalize and compute durations
|
|
68
|
+
|
|
69
|
+
### 🌐 Timezone utilities
|
|
70
|
+
|
|
71
|
+
- Validate IANA timezones
|
|
72
|
+
- Get offsets and compare zones
|
|
73
|
+
- Format in specific timezone
|
|
74
|
+
- Convert absolute moment to zone components
|
|
75
|
+
- Reinterpret wall-clock times
|
|
76
|
+
|
|
77
|
+
### 🕘 Working hours utilities
|
|
78
|
+
|
|
79
|
+
- Define working day patterns and breaks
|
|
80
|
+
- Check working day/time
|
|
81
|
+
- Compute working time between dates
|
|
82
|
+
- Add working hours across days
|
|
83
|
+
- Find next working time
|
|
84
|
+
|
|
85
|
+
### 🎯 Range preset utilities
|
|
86
|
+
|
|
87
|
+
- Today / yesterday / tomorrow
|
|
88
|
+
- Last/next N days windows
|
|
89
|
+
- This/last/next week, month, quarter, year
|
|
90
|
+
- Rolling windows and quarter helpers
|
|
91
|
+
|
|
92
|
+
### 🧱 Constants
|
|
93
|
+
|
|
94
|
+
- Milliseconds & seconds per unit
|
|
95
|
+
- Time unit and formatting option types
|
|
96
|
+
|
|
97
|
+
## 📦 Installation
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
npm install ts-time-utils
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## 🔧 Usage
|
|
104
|
+
|
|
105
|
+
### Import everything (not recommended for production)
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { formatDuration, timeAgo, isValidDate } from "ts-time-utils";
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Import by category (better for tree-shaking)
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import { formatDuration, timeAgo } from "ts-time-utils/format";
|
|
115
|
+
import { differenceInUnits, addTime } from "ts-time-utils/calculate";
|
|
116
|
+
import { isValidDate, isLeapYear } from "ts-time-utils/validate";
|
|
117
|
+
import { calculateAge, getNextBirthday } from "ts-time-utils/age";
|
|
118
|
+
import { getWeekNumber, getQuarter } from "ts-time-utils/calendar";
|
|
119
|
+
import { parseDate, parseRelativeDate } from "ts-time-utils/parse";
|
|
120
|
+
import { sleep, benchmark, Stopwatch } from "ts-time-utils/performance";
|
|
121
|
+
import { createInterval, mergeIntervals } from "ts-time-utils/interval";
|
|
122
|
+
import { formatInTimeZone } from "ts-time-utils/timezone";
|
|
123
|
+
import { isWorkingTime, addWorkingHours } from "ts-time-utils/workingHours";
|
|
124
|
+
import { today, lastNDays } from "ts-time-utils/rangePresets";
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## 📖 Examples
|
|
128
|
+
|
|
129
|
+
### Format Utilities
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { formatDuration, timeAgo, parseDuration } from "ts-time-utils/format";
|
|
133
|
+
|
|
134
|
+
// Format durations
|
|
135
|
+
formatDuration(65000); // "1 minute, 5 seconds"
|
|
136
|
+
formatDuration(65000, { short: true }); // "1m 5s"
|
|
137
|
+
formatDuration(90061000, { maxUnits: 2 }); // "1 day, 1 hour"
|
|
138
|
+
|
|
139
|
+
// Time ago strings
|
|
140
|
+
timeAgo(new Date(Date.now() - 60000)); // "1 minute ago"
|
|
141
|
+
timeAgo(new Date(Date.now() + 60000)); // "in 1 minute"
|
|
142
|
+
|
|
143
|
+
// Parse duration strings
|
|
144
|
+
parseDuration("1h 30m"); // 5400000 (milliseconds)
|
|
145
|
+
parseDuration("2 days 3 hours"); // 183600000
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Calculate Utilities
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import { differenceInUnits, addTime, startOf } from "ts-time-utils/calculate";
|
|
152
|
+
|
|
153
|
+
// Date calculations
|
|
154
|
+
differenceInUnits(new Date("2025-09-01"), new Date("2025-09-11"), "days"); // 10
|
|
155
|
+
|
|
156
|
+
addTime(new Date(), 5, "hours"); // 5 hours from now
|
|
157
|
+
startOf(new Date(), "day"); // Start of today (00:00:00)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Validation Utilities
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
import { isValidDate, isLeapYear, isWeekend } from "ts-time-utils/validate";
|
|
164
|
+
|
|
165
|
+
// Validations
|
|
166
|
+
isValidDate(new Date("2025-13-01")); // false
|
|
167
|
+
isLeapYear(2024); // true
|
|
168
|
+
isWeekend(new Date("2025-09-13")); // true (Saturday)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Age Utilities
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import {
|
|
175
|
+
calculateAge,
|
|
176
|
+
getLifeStage,
|
|
177
|
+
getNextBirthday,
|
|
178
|
+
isBirthday,
|
|
179
|
+
} from "ts-time-utils/age";
|
|
180
|
+
|
|
181
|
+
// Age calculations
|
|
182
|
+
calculateAge(new Date("1990-05-15")); // { years: 34, months: 4, days: 2 }
|
|
183
|
+
getLifeStage(25); // "adult"
|
|
184
|
+
getNextBirthday(new Date("1990-05-15")); // Next May 15th
|
|
185
|
+
isBirthday(new Date("1990-05-15"), new Date("2025-05-15")); // true
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Calendar Utilities
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
import {
|
|
192
|
+
getWeekNumber,
|
|
193
|
+
getQuarter,
|
|
194
|
+
getEaster,
|
|
195
|
+
getDaysInMonth,
|
|
196
|
+
} from "ts-time-utils/calendar";
|
|
197
|
+
|
|
198
|
+
// Calendar operations
|
|
199
|
+
getWeekNumber(new Date("2025-01-15")); // 3
|
|
200
|
+
getQuarter(new Date("2025-07-15")); // 3
|
|
201
|
+
getEaster(2025); // Date object for Easter Sunday 2025
|
|
202
|
+
getDaysInMonth(2, 2024); // 29 (leap year)
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Parse Utilities
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
import {
|
|
209
|
+
parseDate,
|
|
210
|
+
parseRelativeDate,
|
|
211
|
+
parseCustomFormat,
|
|
212
|
+
} from "ts-time-utils/parse";
|
|
213
|
+
|
|
214
|
+
// Advanced parsing
|
|
215
|
+
parseDate("2025-02-30"); // null (invalid date)
|
|
216
|
+
parseDate("Dec 25, 2025"); // Date object
|
|
217
|
+
parseRelativeDate("tomorrow"); // Date for tomorrow
|
|
218
|
+
parseCustomFormat("25/12/2025", "DD/MM/YYYY"); // Date object
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Performance Utilities
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
import {
|
|
225
|
+
sleep,
|
|
226
|
+
timeout,
|
|
227
|
+
benchmark,
|
|
228
|
+
Stopwatch,
|
|
229
|
+
debounce,
|
|
230
|
+
} from "ts-time-utils/performance";
|
|
231
|
+
|
|
232
|
+
// Async utilities
|
|
233
|
+
await sleep(1000); // Wait 1 second
|
|
234
|
+
await timeout(promise, 5000); // Timeout after 5 seconds
|
|
235
|
+
|
|
236
|
+
// Performance measurement
|
|
237
|
+
const result = await benchmark(() => heavyOperation(), 10); // Run 10 times
|
|
238
|
+
const stopwatch = new Stopwatch();
|
|
239
|
+
stopwatch.start();
|
|
240
|
+
// ... operations
|
|
241
|
+
console.log(stopwatch.getElapsed()); // Get elapsed time
|
|
242
|
+
|
|
243
|
+
// Function utilities
|
|
244
|
+
const debouncedFn = debounce(() => console.log("Called!"), 300);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Interval Utilities
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
import {
|
|
251
|
+
createInterval,
|
|
252
|
+
intervalsOverlap,
|
|
253
|
+
mergeIntervals,
|
|
254
|
+
} from "ts-time-utils/interval";
|
|
255
|
+
|
|
256
|
+
const a = createInterval("2025-01-01", "2025-01-05");
|
|
257
|
+
const b = createInterval("2025-01-04", "2025-01-10");
|
|
258
|
+
intervalsOverlap(a!, b!); // true
|
|
259
|
+
const merged = mergeIntervals([a!, b!]);
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Timezone Utilities
|
|
263
|
+
|
|
264
|
+
```ts
|
|
265
|
+
import { formatInTimeZone, getTimezoneOffset } from "ts-time-utils/timezone";
|
|
266
|
+
formatInTimeZone(new Date(), "Europe/Paris", {
|
|
267
|
+
hour: "2-digit",
|
|
268
|
+
minute: "2-digit",
|
|
269
|
+
});
|
|
270
|
+
getTimezoneOffset("America/New_York"); // e.g. -300 (minutes)
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### Working Hours Utilities
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
import { isWorkingTime, addWorkingHours } from "ts-time-utils/workingHours";
|
|
277
|
+
|
|
278
|
+
isWorkingTime(new Date()); // depends on config
|
|
279
|
+
addWorkingHours(new Date(), 10); // adds 10 working hours, skipping off-hours
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Range Preset Utilities
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
import { lastNDays, thisWeek, quarterRange } from "ts-time-utils/rangePresets";
|
|
286
|
+
|
|
287
|
+
const last7 = lastNDays(7);
|
|
288
|
+
const week = thisWeek();
|
|
289
|
+
const quarter = quarterRange();
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## 📊 API Reference
|
|
293
|
+
|
|
294
|
+
### Format Functions
|
|
295
|
+
|
|
296
|
+
- `formatDuration(ms, options?)` - Format milliseconds to readable duration
|
|
297
|
+
- `timeAgo(date, options?)` - Get "time ago" string for past/future dates
|
|
298
|
+
- `formatTime(date, format?)` - Format time as 12h/24h/ISO
|
|
299
|
+
- `parseDuration(duration)` - Parse duration string to milliseconds
|
|
300
|
+
|
|
301
|
+
### Calculate Functions
|
|
302
|
+
|
|
303
|
+
- `differenceInUnits(date1, date2, unit?, precise?)` - Calculate difference between dates
|
|
304
|
+
- `addTime(date, amount, unit)` - Add time to a date
|
|
305
|
+
- `subtractTime(date, amount, unit)` - Subtract time from a date
|
|
306
|
+
- `startOf(date, unit)` - Get start of time period
|
|
307
|
+
- `endOf(date, unit)` - Get end of time period
|
|
308
|
+
- `isBetween(date, start, end)` - Check if date is between two dates
|
|
309
|
+
- `businessDaysBetween(start, end)` - Count business days between dates
|
|
310
|
+
|
|
311
|
+
### Validation Functions
|
|
312
|
+
|
|
313
|
+
- `isValidDate(date)` - Check if date is valid
|
|
314
|
+
- `isLeapYear(year)` - Check if year is leap year
|
|
315
|
+
- `isPast(date)` / `isFuture(date)` - Check if date is past/future
|
|
316
|
+
- `isToday(date)` / `isYesterday(date)` / `isTomorrow(date)` - Date comparisons
|
|
317
|
+
- `isSameDay(date1, date2)` - Check if dates are same day
|
|
318
|
+
- `isWeekend(date)` / `isWeekday(date)` - Check day type
|
|
319
|
+
- `isValidTimeString(time)` - Validate HH:MM time format
|
|
320
|
+
- `isValidISOString(dateString)` - Validate ISO 8601 date string
|
|
321
|
+
|
|
322
|
+
## 🛠️ Development
|
|
323
|
+
|
|
324
|
+
```bash
|
|
325
|
+
# Install dependencies
|
|
326
|
+
npm install
|
|
327
|
+
|
|
328
|
+
# Build (both CommonJS and ES modules)
|
|
329
|
+
npm run build
|
|
330
|
+
|
|
331
|
+
# Run tests
|
|
332
|
+
npm test
|
|
333
|
+
|
|
334
|
+
# Run tests in watch mode
|
|
335
|
+
npm run test:watch
|
|
336
|
+
|
|
337
|
+
# Lint code
|
|
338
|
+
npm run lint
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## 📄 License
|
|
342
|
+
|
|
343
|
+
MIT
|
package/dist/age.d.ts
ADDED
|
@@ -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"}
|
package/dist/age.js
ADDED
|
@@ -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"}
|