chronos-ts 1.1.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.
@@ -0,0 +1,460 @@
1
+ /**
2
+ * Chronos - The ultimate TypeScript date/time library
3
+ * @module Chronos
4
+ */
5
+ import { DateInput, ChronosConfig, Duration, AnyTimeUnit, DayOfWeek, DateTimeSetter, DiffResult, HumanDiffOptions, ChronosLike, DateTimeComponents, ChronosJSON } from '../types';
6
+ /**
7
+ * Chronos - A comprehensive date/time manipulation library
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // Create instances
12
+ * const now = Chronos.now();
13
+ * const date = Chronos.parse('2024-01-15');
14
+ * const birthday = Chronos.create(1990, 5, 15);
15
+ *
16
+ * // Manipulate
17
+ * const future = now.add(3, 'months').startOf('day');
18
+ * const past = now.subtract(1, 'year').endOf('month');
19
+ *
20
+ * // Compare
21
+ * const isAfter = date.isAfter(birthday);
22
+ * const diff = date.diff(birthday, 'years');
23
+ *
24
+ * // Format
25
+ * const formatted = now.format('YYYY-MM-DD HH:mm:ss');
26
+ * const relative = birthday.fromNow(); // "34 years ago"
27
+ * ```
28
+ */
29
+ export declare class Chronos implements ChronosLike {
30
+ private readonly _date;
31
+ private _locale;
32
+ private _timezone?;
33
+ /**
34
+ * Create a new Chronos instance
35
+ *
36
+ * @param input - Date input (string, number, Date, or Chronos)
37
+ * @param timezone - Optional timezone
38
+ */
39
+ private constructor();
40
+ /**
41
+ * Parse a date string
42
+ */
43
+ private parseString;
44
+ /**
45
+ * Helper to create a date from components in a specific timezone
46
+ */
47
+ private static dateFromComponents;
48
+ /**
49
+ * Create a Chronos instance from various inputs
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * Chronos.parse('2024-01-15')
54
+ * Chronos.parse(1705276800000)
55
+ * Chronos.parse(new Date())
56
+ * ```
57
+ */
58
+ static parse(input?: DateInput, timezone?: string): Chronos;
59
+ /**
60
+ * Create a Chronos instance for the current moment
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const now = Chronos.now();
65
+ * ```
66
+ */
67
+ static now(timezone?: string): Chronos;
68
+ /**
69
+ * Create a Chronos instance for today at midnight
70
+ */
71
+ static today(timezone?: string): Chronos;
72
+ /**
73
+ * Create a Chronos instance for tomorrow at midnight
74
+ */
75
+ static tomorrow(timezone?: string): Chronos;
76
+ /**
77
+ * Create a Chronos instance for yesterday at midnight
78
+ */
79
+ static yesterday(timezone?: string): Chronos;
80
+ /**
81
+ * Create a Chronos instance from individual components
82
+ * Month is 1-12 (like Carbon PHP)
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * Chronos.create(2024, 1, 15, 10, 30, 0) // Jan 15, 2024 10:30:00
87
+ * ```
88
+ */
89
+ static create(year: number, month?: number, day?: number, hour?: number, minute?: number, second?: number, millisecond?: number, timezone?: string): Chronos;
90
+ /**
91
+ * Create a Chronos instance from a Unix timestamp (seconds)
92
+ */
93
+ static fromUnix(timestamp: number, timezone?: string): Chronos;
94
+ /**
95
+ * Create a Chronos instance from a Unix timestamp (milliseconds)
96
+ */
97
+ static fromMillis(timestamp: number, timezone?: string): Chronos;
98
+ /**
99
+ * Create a Chronos instance from components object
100
+ * Month in components is 1-12 (like Carbon PHP)
101
+ */
102
+ static fromObject(components: DateTimeComponents, timezone?: string): Chronos;
103
+ /**
104
+ * Create a Chronos instance from a format string
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * Chronos.fromFormat('15-01-2024', 'DD-MM-YYYY')
109
+ * ```
110
+ */
111
+ static fromFormat(input: string, format: string, timezone?: string): Chronos;
112
+ /**
113
+ * Create the minimum possible date
114
+ */
115
+ static min(): Chronos;
116
+ /**
117
+ * Create the maximum possible date
118
+ */
119
+ static max(): Chronos;
120
+ /**
121
+ * Get the earliest of multiple dates
122
+ */
123
+ static earliest(...dates: (DateInput | Chronos)[]): Chronos;
124
+ /**
125
+ * Get the latest of multiple dates
126
+ */
127
+ static latest(...dates: (DateInput | Chronos)[]): Chronos;
128
+ /** Get the year */
129
+ get year(): number;
130
+ /** Get the month (1-12) */
131
+ get month(): number;
132
+ /** Get the day of month (1-31) */
133
+ get date(): number;
134
+ /** Alias for date */
135
+ get day(): number;
136
+ /** Get the day of week (0-6, Sunday = 0) */
137
+ get dayOfWeek(): DayOfWeek;
138
+ /** Get the hour (0-23) */
139
+ get hour(): number;
140
+ /** Get the minute (0-59) */
141
+ get minute(): number;
142
+ /** Get the second (0-59) */
143
+ get second(): number;
144
+ /** Get the millisecond (0-999) */
145
+ get millisecond(): number;
146
+ /** Get Unix timestamp (seconds) */
147
+ get unix(): number;
148
+ /** Get Unix timestamp (milliseconds) */
149
+ get timestamp(): number;
150
+ /** Get the quarter (1-4) */
151
+ get quarter(): number;
152
+ /** Get the day of year (1-366) */
153
+ get dayOfYear(): number;
154
+ /** Get the ISO week number (1-53) */
155
+ get week(): number;
156
+ /** Get the ISO week year */
157
+ get weekYear(): number;
158
+ /** Get the number of days in the current month */
159
+ get daysInMonth(): number;
160
+ /** Get the number of days in the current year */
161
+ get daysInYear(): number;
162
+ /** Get the number of weeks in the current year */
163
+ get weeksInYear(): number;
164
+ /** Check if the year is a leap year */
165
+ get isLeapYear(): boolean;
166
+ /** Get the timezone offset in minutes */
167
+ get offset(): number;
168
+ /** Get the timezone offset as string (+05:30) */
169
+ get offsetString(): string;
170
+ /**
171
+ * Set specific date/time components
172
+ * Returns a new Chronos instance
173
+ */
174
+ /**
175
+ * Set multiple date/time values at once
176
+ * Month is 1-12 (like Carbon PHP)
177
+ */
178
+ set(values: DateTimeSetter): Chronos;
179
+ /** Set the year */
180
+ setYear(year: number): Chronos;
181
+ /** Set the month (1-12) */
182
+ setMonth(month: number): Chronos;
183
+ /** Set the day of month (1-31) */
184
+ setDate(date: number): Chronos;
185
+ /** Set the hour (0-23) */
186
+ setHour(hour: number): Chronos;
187
+ /** Set the minute (0-59) */
188
+ setMinute(minute: number): Chronos;
189
+ /** Set the second (0-59) */
190
+ setSecond(second: number): Chronos;
191
+ /** Set the millisecond (0-999) */
192
+ setMillisecond(millisecond: number): Chronos;
193
+ /**
194
+ * Add time to the date
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * chronos.add(5, 'days')
199
+ * chronos.add(2, 'months')
200
+ * chronos.add({ years: 1, months: 2 })
201
+ * ```
202
+ */
203
+ add(amount: number | Duration, unit?: AnyTimeUnit): Chronos;
204
+ /**
205
+ * Subtract time from the date
206
+ */
207
+ subtract(amount: number | Duration, unit?: AnyTimeUnit): Chronos;
208
+ /**
209
+ * Get the start of a time unit
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * chronos.startOf('day') // 00:00:00.000
214
+ * chronos.startOf('month') // First day of month
215
+ * chronos.startOf('year') // January 1st
216
+ * ```
217
+ */
218
+ startOf(unit: AnyTimeUnit): Chronos;
219
+ /**
220
+ * Get the end of a time unit
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * chronos.endOf('day') // 23:59:59.999
225
+ * chronos.endOf('month') // Last day of month
226
+ * chronos.endOf('year') // December 31st
227
+ * ```
228
+ */
229
+ endOf(unit: AnyTimeUnit): Chronos;
230
+ addMilliseconds(amount: number): Chronos;
231
+ addSeconds(amount: number): Chronos;
232
+ addMinutes(amount: number): Chronos;
233
+ addHours(amount: number): Chronos;
234
+ addDays(amount: number): Chronos;
235
+ addWeeks(amount: number): Chronos;
236
+ addMonths(amount: number): Chronos;
237
+ addQuarters(amount: number): Chronos;
238
+ addYears(amount: number): Chronos;
239
+ subtractMilliseconds(amount: number): Chronos;
240
+ subtractSeconds(amount: number): Chronos;
241
+ subtractMinutes(amount: number): Chronos;
242
+ subtractHours(amount: number): Chronos;
243
+ subtractDays(amount: number): Chronos;
244
+ subtractWeeks(amount: number): Chronos;
245
+ subtractMonths(amount: number): Chronos;
246
+ subtractQuarters(amount: number): Chronos;
247
+ subtractYears(amount: number): Chronos;
248
+ /**
249
+ * Check if this date is before another
250
+ */
251
+ isBefore(other: DateInput, unit?: AnyTimeUnit): boolean;
252
+ /**
253
+ * Check if this date is after another
254
+ */
255
+ isAfter(other: DateInput, unit?: AnyTimeUnit): boolean;
256
+ /**
257
+ * Check if this date is the same as another
258
+ */
259
+ isSame(other: DateInput, unit?: AnyTimeUnit): boolean;
260
+ /**
261
+ * Check if this date is same or before another
262
+ */
263
+ isSameOrBefore(other: DateInput, unit?: AnyTimeUnit): boolean;
264
+ /**
265
+ * Check if this date is same or after another
266
+ */
267
+ isSameOrAfter(other: DateInput, unit?: AnyTimeUnit): boolean;
268
+ /**
269
+ * Check if this date is between two others
270
+ */
271
+ isBetween(start: DateInput, end: DateInput, unit?: AnyTimeUnit, inclusivity?: '()' | '[]' | '[)' | '(]'): boolean;
272
+ /** Check if this date is today */
273
+ isToday(): boolean;
274
+ /** Check if this date is tomorrow */
275
+ isTomorrow(): boolean;
276
+ /** Check if this date is yesterday */
277
+ isYesterday(): boolean;
278
+ /** Check if this date is in the past */
279
+ isPast(): boolean;
280
+ /** Check if this date is in the future */
281
+ isFuture(): boolean;
282
+ /** Check if this is a weekend (Saturday or Sunday) */
283
+ isWeekend(): boolean;
284
+ /** Check if this is a weekday (Monday-Friday) */
285
+ isWeekday(): boolean;
286
+ isSunday(): boolean;
287
+ isMonday(): boolean;
288
+ isTuesday(): boolean;
289
+ isWednesday(): boolean;
290
+ isThursday(): boolean;
291
+ isFriday(): boolean;
292
+ isSaturday(): boolean;
293
+ /**
294
+ * Get the difference between two dates in a specific unit
295
+ */
296
+ diff(other: DateInput, unit?: AnyTimeUnit, precise?: boolean): number;
297
+ /**
298
+ * Get a detailed diff breakdown
299
+ */
300
+ diffDetailed(other: DateInput): DiffResult;
301
+ /**
302
+ * Get a human-readable relative time string
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * date.fromNow() // "2 days ago"
307
+ * date.from(other) // "in 3 months"
308
+ * date.fromNow({ short: true }) // "2d ago"
309
+ * ```
310
+ */
311
+ fromNow(options?: HumanDiffOptions): string;
312
+ /**
313
+ * Get relative time from another date
314
+ */
315
+ from(other: DateInput, options?: HumanDiffOptions): string;
316
+ /**
317
+ * Get relative time to another date
318
+ */
319
+ to(other: DateInput, options?: HumanDiffOptions): string;
320
+ /**
321
+ * Get relative time to now
322
+ */
323
+ toNow(options?: HumanDiffOptions): string;
324
+ /**
325
+ * Format the date using a format string
326
+ *
327
+ * Supported tokens:
328
+ * - YYYY: 4-digit year
329
+ * - YY: 2-digit year
330
+ * - MMMM: Full month name
331
+ * - MMM: Short month name
332
+ * - MM: 2-digit month
333
+ * - M: 1-2 digit month
334
+ * - DD: 2-digit day
335
+ * - D: 1-2 digit day
336
+ * - dddd: Full weekday name
337
+ * - ddd: Short weekday name
338
+ * - dd: Min weekday name
339
+ * - d: Day of week number
340
+ * - HH: 2-digit hour (24h)
341
+ * - H: 1-2 digit hour (24h)
342
+ * - hh: 2-digit hour (12h)
343
+ * - h: 1-2 digit hour (12h)
344
+ * - mm: 2-digit minute
345
+ * - m: 1-2 digit minute
346
+ * - ss: 2-digit second
347
+ * - s: 1-2 digit second
348
+ * - SSS: 3-digit millisecond
349
+ * - A: AM/PM
350
+ * - a: am/pm
351
+ * - Z: Timezone offset (+05:00)
352
+ * - ZZ: Timezone offset (+0500)
353
+ */
354
+ format(formatStr?: string): string;
355
+ /** Format as ISO 8601 */
356
+ toISOString(): string;
357
+ /** Format as ISO date (YYYY-MM-DD) */
358
+ toDateString(): string;
359
+ /** Format as time (HH:mm:ss) */
360
+ toTimeString(): string;
361
+ /** Format as datetime (YYYY-MM-DD HH:mm:ss) */
362
+ toDateTimeString(): string;
363
+ /** Format as RFC 2822 */
364
+ toRFC2822(): string;
365
+ /** Format as RFC 3339 */
366
+ toRFC3339(): string;
367
+ /** Format as ATOM */
368
+ toAtomString(): string;
369
+ /** Format as Cookie */
370
+ toCookieString(): string;
371
+ /** Format as RSS */
372
+ toRSSString(): string;
373
+ /** Format as W3C */
374
+ toW3CString(): string;
375
+ /**
376
+ * Convert to a specific timezone
377
+ */
378
+ toTimezone(timezone: string): Chronos;
379
+ /** Convert to native Date object */
380
+ toDate(): Date;
381
+ /** Convert to array [year, month, day, hour, minute, second, millisecond] */
382
+ toArray(): number[];
383
+ /** Convert to object */
384
+ toObject(): DateTimeComponents;
385
+ /** Convert to JSON-serializable object */
386
+ toJSON(): ChronosJSON;
387
+ /** Get primitive value (timestamp) */
388
+ valueOf(): number;
389
+ /** Convert to string */
390
+ toString(): string;
391
+ /**
392
+ * Create a clone of this instance
393
+ */
394
+ clone(): Chronos;
395
+ /**
396
+ * Set the locale for this instance
397
+ */
398
+ locale(code: string): Chronos;
399
+ /**
400
+ * Get the current locale code
401
+ */
402
+ getLocale(): string;
403
+ /**
404
+ * Get the next occurrence of a specific day
405
+ */
406
+ next(day: DayOfWeek): Chronos;
407
+ /**
408
+ * Get the previous occurrence of a specific day
409
+ */
410
+ previous(day: DayOfWeek): Chronos;
411
+ /**
412
+ * Get the closest date (either this or other)
413
+ */
414
+ closest(date1: DateInput, date2: DateInput): Chronos;
415
+ /**
416
+ * Get the farthest date (either this or other)
417
+ */
418
+ farthest(date1: DateInput, date2: DateInput): Chronos;
419
+ /**
420
+ * Set global configuration
421
+ */
422
+ static configure(config: Partial<ChronosConfig>): void;
423
+ /**
424
+ * Get current global configuration
425
+ */
426
+ static getConfig(): ChronosConfig;
427
+ /**
428
+ * Set test time (for testing purposes)
429
+ */
430
+ static setTestNow(date?: DateInput): void;
431
+ /**
432
+ * Check if test mode is active
433
+ */
434
+ static hasTestNow(): boolean;
435
+ /**
436
+ * Get the test time
437
+ */
438
+ static getTestNow(): Chronos | null;
439
+ /**
440
+ * Check if the date is valid
441
+ */
442
+ isValid(): boolean;
443
+ /**
444
+ * Check if this date is the same day as another (regardless of time)
445
+ */
446
+ isSameDay(other: DateInput): boolean;
447
+ /**
448
+ * Check if this date is in the same month as another
449
+ */
450
+ isSameMonth(other: DateInput): boolean;
451
+ /**
452
+ * Check if this date is in the same year as another
453
+ */
454
+ isSameYear(other: DateInput): boolean;
455
+ /**
456
+ * Get calendar output for the current month
457
+ */
458
+ calendar(referenceDate?: DateInput): string;
459
+ }
460
+ export default Chronos;