@bereasoftware/time-guard 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +315 -315
- package/dist/calendars/index.cjs +2 -1
- package/dist/calendars/index.cjs.map +1 -0
- package/dist/calendars/index.es.js +3 -1
- package/dist/calendars/index.es.js.map +1 -0
- package/dist/locales/index.cjs +2 -1
- package/dist/locales/index.cjs.map +1 -0
- package/dist/locales/index.es.js +1733 -1733
- package/dist/locales/index.es.js.map +1 -0
- package/dist/plugins/advanced-format.cjs +2 -1
- package/dist/plugins/advanced-format.cjs.map +1 -0
- package/dist/plugins/advanced-format.es.js +7 -5
- package/dist/plugins/advanced-format.es.js.map +1 -0
- package/dist/plugins/duration.cjs +2 -1
- package/dist/plugins/duration.cjs.map +1 -0
- package/dist/plugins/duration.es.js +3 -1
- package/dist/plugins/duration.es.js.map +1 -0
- package/dist/plugins/relative-time.cjs +2 -1
- package/dist/plugins/relative-time.cjs.map +1 -0
- package/dist/plugins/relative-time.es.js +3 -1
- package/dist/plugins/relative-time.es.js.map +1 -0
- package/dist/time-guard.cjs +2 -1
- package/dist/time-guard.cjs.map +1 -0
- package/dist/time-guard.es.js +1858 -1908
- package/dist/time-guard.es.js.map +1 -0
- package/dist/time-guard.iife.js +2 -1
- package/dist/time-guard.iife.js.map +1 -0
- package/dist/time-guard.umd.js +2 -1
- package/dist/time-guard.umd.js.map +1 -0
- package/dist/types/time-guard.d.ts +1 -5
- package/dist/types/utils/duration-locale.d.ts +33 -0
- package/package.json +21 -7
- package/dist/bereasoftware-time-guard-2.3.0.tgz +0 -0
package/README.md
CHANGED
|
@@ -62,21 +62,21 @@ Una biblioteca moderna y de nivel producción para manipulación de fechas/horas
|
|
|
62
62
|
## 🚀 Inicio Rápido
|
|
63
63
|
|
|
64
64
|
```typescript
|
|
65
|
-
import { TimeGuard } from
|
|
65
|
+
import { TimeGuard } from '@bereasoftware/time-guard';
|
|
66
66
|
|
|
67
67
|
// Crear una fecha
|
|
68
68
|
const now = TimeGuard.now();
|
|
69
|
-
const date = TimeGuard.from(
|
|
69
|
+
const date = TimeGuard.from('2024-03-13');
|
|
70
70
|
|
|
71
71
|
// Manipular fechas
|
|
72
|
-
const tomorrow = date.add(1,
|
|
73
|
-
const nextMonth = date.add(1,
|
|
72
|
+
const tomorrow = date.add(1, 'day');
|
|
73
|
+
const nextMonth = date.add(1, 'month');
|
|
74
74
|
|
|
75
75
|
// Formatear con locales
|
|
76
|
-
const spanish = date.locale(
|
|
76
|
+
const spanish = date.locale('es').format('dddd, DD MMMM YYYY');
|
|
77
77
|
// Resultado: miércoles, 13 marzo 2024
|
|
78
78
|
|
|
79
|
-
const japanese = date.locale(
|
|
79
|
+
const japanese = date.locale('ja').format('YYYY年M月D日');
|
|
80
80
|
// Resultado: 2024年3月13日
|
|
81
81
|
|
|
82
82
|
// Obtener componentes
|
|
@@ -88,7 +88,7 @@ console.log(date.dayOfWeek()); // 3 (miércoles)
|
|
|
88
88
|
// Comparar fechas
|
|
89
89
|
console.log(date.isBefore(tomorrow)); // true
|
|
90
90
|
console.log(date.isSame(date.clone())); // true
|
|
91
|
-
console.log(date.isAfter(new Date(
|
|
91
|
+
console.log(date.isAfter(new Date('2020-01-01'))); // true
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
---
|
|
@@ -115,14 +115,14 @@ TimeGuard usa una arquitectura modular inspirada en dayjs. El **core** pesa ~5KB
|
|
|
115
115
|
|
|
116
116
|
```typescript
|
|
117
117
|
// Core ligero (~5KB gzip) - solo EN/ES
|
|
118
|
-
import { TimeGuard } from
|
|
118
|
+
import { TimeGuard } from '@bereasoftware/time-guard';
|
|
119
119
|
|
|
120
120
|
// Módulos bajo demanda
|
|
121
|
-
import { ALL_LOCALES } from
|
|
122
|
-
import { IslamicCalendar } from
|
|
123
|
-
import relativeTimePlugin from
|
|
124
|
-
import { Duration } from
|
|
125
|
-
import advancedFormatPlugin from
|
|
121
|
+
import { ALL_LOCALES } from '@bereasoftware/time-guard/locales';
|
|
122
|
+
import { IslamicCalendar } from '@bereasoftware/time-guard/calendars';
|
|
123
|
+
import relativeTimePlugin from '@bereasoftware/time-guard/plugins/relative-time';
|
|
124
|
+
import { Duration } from '@bereasoftware/time-guard/plugins/duration';
|
|
125
|
+
import advancedFormatPlugin from '@bereasoftware/time-guard/plugins/advanced-format';
|
|
126
126
|
|
|
127
127
|
// UMD para CDN / <script>
|
|
128
128
|
// <script src="unpkg.com/@bereasoftware/time-guard/dist/time-guard.umd.js"></script>
|
|
@@ -139,8 +139,8 @@ import advancedFormatPlugin from "@bereasoftware/time-guard/plugins/advanced-for
|
|
|
139
139
|
Todas las instancias de TimeGuard son inmutables. Cada operación devuelve una nueva instancia:
|
|
140
140
|
|
|
141
141
|
```typescript
|
|
142
|
-
const date = TimeGuard.from(
|
|
143
|
-
const modified = date.add(1,
|
|
142
|
+
const date = TimeGuard.from('2024-03-13');
|
|
143
|
+
const modified = date.add(1, 'day');
|
|
144
144
|
|
|
145
145
|
console.log(date.day()); // 13 (sin cambios)
|
|
146
146
|
console.log(modified.day()); // 14 (nueva instancia)
|
|
@@ -151,14 +151,14 @@ console.log(modified.day()); // 14 (nueva instancia)
|
|
|
151
151
|
Maneja zonas horarias con soporte completo de Temporal API:
|
|
152
152
|
|
|
153
153
|
```typescript
|
|
154
|
-
const date = TimeGuard.from(
|
|
154
|
+
const date = TimeGuard.from('2024-03-13 10:30:00');
|
|
155
155
|
|
|
156
156
|
// Set timezone
|
|
157
|
-
const inNYC = date.timezone(
|
|
158
|
-
const inTokyo = date.timezone(
|
|
157
|
+
const inNYC = date.timezone('America/New_York');
|
|
158
|
+
const inTokyo = date.timezone('Asia/Tokyo');
|
|
159
159
|
|
|
160
|
-
console.log(inNYC.format(
|
|
161
|
-
console.log(inTokyo.format(
|
|
160
|
+
console.log(inNYC.format('YYYY-MM-DD HH:mm:ss Z'));
|
|
161
|
+
console.log(inTokyo.format('YYYY-MM-DD HH:mm:ss Z'));
|
|
162
162
|
```
|
|
163
163
|
|
|
164
164
|
### 3. Localización
|
|
@@ -166,15 +166,15 @@ console.log(inTokyo.format("YYYY-MM-DD HH:mm:ss Z"));
|
|
|
166
166
|
Soporte para 40+ idiomas y locales:
|
|
167
167
|
|
|
168
168
|
```typescript
|
|
169
|
-
const date = TimeGuard.from(
|
|
170
|
-
|
|
171
|
-
date.locale(
|
|
172
|
-
date.locale(
|
|
173
|
-
date.locale(
|
|
174
|
-
date.locale(
|
|
175
|
-
date.locale(
|
|
176
|
-
date.locale(
|
|
177
|
-
date.locale(
|
|
169
|
+
const date = TimeGuard.from('2024-03-13');
|
|
170
|
+
|
|
171
|
+
date.locale('en').format('MMMM DD, YYYY'); // March 13, 2024
|
|
172
|
+
date.locale('es').format('DD MMMM YYYY'); // 13 marzo 2024
|
|
173
|
+
date.locale('fr').format('DD MMMM YYYY'); // 13 mars 2024
|
|
174
|
+
date.locale('de').format('DD. MMMM YYYY'); // 13. März 2024
|
|
175
|
+
date.locale('ja').format('YYYY年M月D日'); // 2024年3月13日
|
|
176
|
+
date.locale('zh-cn').format('YYYY年M月D日'); // 2024年3月13日
|
|
177
|
+
date.locale('ar').format('DD MMMM YYYY'); // 13 مارس 2024
|
|
178
178
|
```
|
|
179
179
|
|
|
180
180
|
### 4. Estrategias de Formato
|
|
@@ -182,21 +182,21 @@ date.locale("ar").format("DD MMMM YYYY"); // 13 مارس 2024
|
|
|
182
182
|
Múltiples formatos preestablecidos y patrones personalizados:
|
|
183
183
|
|
|
184
184
|
```typescript
|
|
185
|
-
const date = TimeGuard.from(
|
|
185
|
+
const date = TimeGuard.from('2024-03-13 14:30:45');
|
|
186
186
|
|
|
187
187
|
// Preestablecidos
|
|
188
|
-
date.format(
|
|
189
|
-
date.format(
|
|
190
|
-
date.format(
|
|
191
|
-
date.format(
|
|
192
|
-
date.format(
|
|
193
|
-
date.format(
|
|
194
|
-
date.format(
|
|
188
|
+
date.format('iso'); // 2024-03-13T14:30:45
|
|
189
|
+
date.format('date'); // 2024-03-13
|
|
190
|
+
date.format('time'); // 14:30:45
|
|
191
|
+
date.format('datetime'); // 2024-03-13 14:30:45
|
|
192
|
+
date.format('rfc2822'); // Wed, 13 Mar 2024 14:30:45 GMT
|
|
193
|
+
date.format('rfc3339'); // 2024-03-13T14:30:45Z
|
|
194
|
+
date.format('utc'); // 2024-03-13T14:30:45Z
|
|
195
195
|
|
|
196
196
|
// Patrones personalizados
|
|
197
|
-
date.format(
|
|
198
|
-
date.format(
|
|
199
|
-
date.format(
|
|
197
|
+
date.format('YYYY-MM-DD HH:mm:ss');
|
|
198
|
+
date.format('dddd, MMMM DD, YYYY');
|
|
199
|
+
date.format('MM/DD/YYYY');
|
|
200
200
|
```
|
|
201
201
|
|
|
202
202
|
---
|
|
@@ -206,7 +206,7 @@ date.format("MM/DD/YYYY");
|
|
|
206
206
|
Acceso rápido a componentes individuales de la fecha:
|
|
207
207
|
|
|
208
208
|
```typescript
|
|
209
|
-
const date = TimeGuard.from(
|
|
209
|
+
const date = TimeGuard.from('2024-03-13 14:30:45.123');
|
|
210
210
|
|
|
211
211
|
// Componentes individuales
|
|
212
212
|
console.log(date.year()); // 2024
|
|
@@ -237,8 +237,8 @@ console.log(date.inLeapYear()); // true
|
|
|
237
237
|
La forma **más semántica y clara** para calcular diferencias de tiempo. ¡No necesitas pensar en `until()` vs `since()`!
|
|
238
238
|
|
|
239
239
|
```typescript
|
|
240
|
-
const start = TimeGuard.from(
|
|
241
|
-
const end = TimeGuard.from(
|
|
240
|
+
const start = TimeGuard.from('2024-01-15');
|
|
241
|
+
const end = TimeGuard.from('2024-03-20');
|
|
242
242
|
|
|
243
243
|
// Lectura simple y directa
|
|
244
244
|
TimeGuard.between(start, end).humanize();
|
|
@@ -253,7 +253,7 @@ TimeGuard.between(start, end).months; // 2
|
|
|
253
253
|
TimeGuard.between(start, end).days; // 5
|
|
254
254
|
|
|
255
255
|
// Todas las opciones de humanize() disponibles
|
|
256
|
-
TimeGuard.between(start, end).humanize({ locale:
|
|
256
|
+
TimeGuard.between(start, end).humanize({ locale: 'es', fullBreakdown: true });
|
|
257
257
|
// "2 meses y 5 días"
|
|
258
258
|
```
|
|
259
259
|
|
|
@@ -275,13 +275,13 @@ TimeGuard.between(end, start); // mismo resultado, order no importa
|
|
|
275
275
|
|
|
276
276
|
```typescript
|
|
277
277
|
// Sintaxis limpia y directa
|
|
278
|
-
TimeGuard.range(
|
|
278
|
+
TimeGuard.range('2024-01-15', '2024-03-20').humanize();
|
|
279
279
|
// "2 months and 5 days"
|
|
280
280
|
|
|
281
|
-
TimeGuard.range(
|
|
281
|
+
TimeGuard.range('2024-01-15', '2024-03-20').inMonths();
|
|
282
282
|
// 2.1355 (valor decimal preciso)
|
|
283
283
|
|
|
284
|
-
TimeGuard.range(
|
|
284
|
+
TimeGuard.range('2024-01-15', '2024-03-20').toDuration();
|
|
285
285
|
// DurationResult: acceso completo a propiedades
|
|
286
286
|
```
|
|
287
287
|
|
|
@@ -289,28 +289,28 @@ TimeGuard.range("2024-01-15", "2024-03-20").toDuration();
|
|
|
289
289
|
|
|
290
290
|
```typescript
|
|
291
291
|
// 📅 Período de alquiler
|
|
292
|
-
const checkIn = new TimeGuard(
|
|
293
|
-
const checkOut = new TimeGuard(
|
|
292
|
+
const checkIn = new TimeGuard('2024-06-15');
|
|
293
|
+
const checkOut = new TimeGuard('2024-06-22');
|
|
294
294
|
|
|
295
|
-
const rentalDays = TimeGuard.range(checkIn, checkOut).in(
|
|
295
|
+
const rentalDays = TimeGuard.range(checkIn, checkOut).in('day');
|
|
296
296
|
const rentalCost = rentalDays * 100; // $100 por día
|
|
297
297
|
console.log(rentalCost); // $700
|
|
298
298
|
|
|
299
299
|
// 💳 Cálculo de mora
|
|
300
|
-
const invoiceDate = new TimeGuard(
|
|
301
|
-
const paymentDate = new TimeGuard(
|
|
300
|
+
const invoiceDate = new TimeGuard('2024-01-01');
|
|
301
|
+
const paymentDate = new TimeGuard('2024-02-15');
|
|
302
302
|
|
|
303
303
|
const lateFeePerDay = 10;
|
|
304
304
|
const lateFee =
|
|
305
|
-
TimeGuard.range(invoiceDate, paymentDate).in(
|
|
305
|
+
TimeGuard.range(invoiceDate, paymentDate).in('day') * lateFeePerDay;
|
|
306
306
|
console.log(lateFee); // Cálculo exacto con promediado de meses
|
|
307
307
|
|
|
308
308
|
// 📊 Analítica de sesiones
|
|
309
|
-
const sessionStart = new TimeGuard(
|
|
310
|
-
const sessionEnd = new TimeGuard(
|
|
309
|
+
const sessionStart = new TimeGuard('2024-03-15T10:00:00');
|
|
310
|
+
const sessionEnd = new TimeGuard('2024-03-15T10:35:42');
|
|
311
311
|
|
|
312
312
|
const engagementMinutes = TimeGuard.range(sessionStart, sessionEnd).in(
|
|
313
|
-
|
|
313
|
+
'minute',
|
|
314
314
|
);
|
|
315
315
|
console.log(engagementMinutes); // Para métricas de usuarios
|
|
316
316
|
```
|
|
@@ -319,19 +319,19 @@ console.log(engagementMinutes); // Para métricas de usuarios
|
|
|
319
319
|
|
|
320
320
|
```typescript
|
|
321
321
|
// El orden de fechas NO importa
|
|
322
|
-
TimeGuard.range(
|
|
322
|
+
TimeGuard.range('2024-03-20', '2024-01-15').humanize({ locale: 'es' });
|
|
323
323
|
// "2 meses y 5 días" (resultado idéntico)
|
|
324
324
|
|
|
325
325
|
// Diferentes idiomas
|
|
326
|
-
TimeGuard.range(
|
|
326
|
+
TimeGuard.range('2024-01-15', '2024-03-20').humanize({ locale: 'fr' });
|
|
327
327
|
// "2 mois et 5 jours"
|
|
328
328
|
|
|
329
|
-
TimeGuard.range(
|
|
329
|
+
TimeGuard.range('2024-01-15', '2024-03-20').humanize({ locale: 'de' });
|
|
330
330
|
// "2 Monate und 5 Tage"
|
|
331
331
|
|
|
332
332
|
// Desglose completo
|
|
333
|
-
TimeGuard.range(
|
|
334
|
-
locale:
|
|
333
|
+
TimeGuard.range('2024-01-15', '2024-03-20').humanize({
|
|
334
|
+
locale: 'es',
|
|
335
335
|
fullBreakdown: true,
|
|
336
336
|
});
|
|
337
337
|
// "2 meses y 5 días"
|
|
@@ -349,8 +349,8 @@ TimeGuard.range("2024-01-15", "2024-03-20").humanize({
|
|
|
349
349
|
### Duración: Calcular tiempo entre fechas
|
|
350
350
|
|
|
351
351
|
```typescript
|
|
352
|
-
const start = TimeGuard.from(
|
|
353
|
-
const end = TimeGuard.from(
|
|
352
|
+
const start = TimeGuard.from('2024-01-15');
|
|
353
|
+
const end = TimeGuard.from('2024-03-20');
|
|
354
354
|
|
|
355
355
|
const duration = start.until(end);
|
|
356
356
|
|
|
@@ -371,8 +371,8 @@ console.log(duration);
|
|
|
371
371
|
Convierte duraciones a texto natural y legible con soporte multiidioma:
|
|
372
372
|
|
|
373
373
|
```typescript
|
|
374
|
-
const start = TimeGuard.from(
|
|
375
|
-
const end = TimeGuard.from(
|
|
374
|
+
const start = TimeGuard.from('2024-01-15');
|
|
375
|
+
const end = TimeGuard.from('2024-03-20');
|
|
376
376
|
|
|
377
377
|
// Estilo simple (Intl.RelativeTimeFormat)
|
|
378
378
|
const duration = start.until(end);
|
|
@@ -384,17 +384,17 @@ console.log(duration.humanize({ fullBreakdown: true }));
|
|
|
384
384
|
// "2 months and 5 days"
|
|
385
385
|
|
|
386
386
|
// Con locale específico
|
|
387
|
-
console.log(duration.humanize({ locale:
|
|
387
|
+
console.log(duration.humanize({ locale: 'es' }));
|
|
388
388
|
// "2 meses"
|
|
389
389
|
|
|
390
|
-
console.log(duration.humanize({ locale:
|
|
390
|
+
console.log(duration.humanize({ locale: 'es', fullBreakdown: true }));
|
|
391
391
|
// "2 meses y 5 días"
|
|
392
392
|
|
|
393
393
|
// Con locale francés
|
|
394
|
-
console.log(duration.humanize({ locale:
|
|
394
|
+
console.log(duration.humanize({ locale: 'fr' }));
|
|
395
395
|
// "2 mois"
|
|
396
396
|
|
|
397
|
-
console.log(duration.humanize({ locale:
|
|
397
|
+
console.log(duration.humanize({ locale: 'fr', fullBreakdown: true }));
|
|
398
398
|
// "2 mois et 5 jours"
|
|
399
399
|
```
|
|
400
400
|
|
|
@@ -402,8 +402,8 @@ console.log(duration.humanize({ locale: "fr", fullBreakdown: true }));
|
|
|
402
402
|
|
|
403
403
|
```typescript
|
|
404
404
|
// TimeGuard hereda configuración de locale
|
|
405
|
-
const start = TimeGuard.from(
|
|
406
|
-
const end = TimeGuard.from(
|
|
405
|
+
const start = TimeGuard.from('2024-01-15', { locale: 'es' });
|
|
406
|
+
const end = TimeGuard.from('2024-03-20');
|
|
407
407
|
|
|
408
408
|
const duration = start.until(end);
|
|
409
409
|
|
|
@@ -448,8 +448,8 @@ Perfecto para:
|
|
|
448
448
|
- ✅ **Validación** - Confirmar que los cálculos son correctos
|
|
449
449
|
|
|
450
450
|
```typescript
|
|
451
|
-
const start = TimeGuard.from(
|
|
452
|
-
const end = TimeGuard.from(
|
|
451
|
+
const start = TimeGuard.from('2024-01-15');
|
|
452
|
+
const end = TimeGuard.from('2024-03-20');
|
|
453
453
|
const duration = start.until(end);
|
|
454
454
|
|
|
455
455
|
// Explicación detallada del cálculo
|
|
@@ -498,16 +498,16 @@ Debugging de lógica de fechas:
|
|
|
498
498
|
|
|
499
499
|
```typescript
|
|
500
500
|
// ¿Por qué el cálculo de mora es diferente?
|
|
501
|
-
const invoiceDate = TimeGuard.from(
|
|
502
|
-
const paymentDate = TimeGuard.from(
|
|
501
|
+
const invoiceDate = TimeGuard.from('2024-01-05');
|
|
502
|
+
const paymentDate = TimeGuard.from('2024-02-15');
|
|
503
503
|
const lateFee = invoiceDate.until(paymentDate);
|
|
504
504
|
|
|
505
505
|
// Obtén la explicación completa
|
|
506
506
|
const debug = lateFee.explain();
|
|
507
507
|
|
|
508
|
-
console.log(
|
|
509
|
-
console.log(
|
|
510
|
-
console.log(
|
|
508
|
+
console.log('Pasos del cálculo:', debug.steps);
|
|
509
|
+
console.log('Febrero es bisiesto:', debug.leapYearFlags);
|
|
510
|
+
console.log('Desglose:', debug.breakdown);
|
|
511
511
|
// Ahora sabes exactamente qué sucedió!
|
|
512
512
|
```
|
|
513
513
|
|
|
@@ -515,8 +515,8 @@ Educación - Enseñar cálculos de fechas:
|
|
|
515
515
|
|
|
516
516
|
```typescript
|
|
517
517
|
// Mostrar a estudiantes cómo funciona
|
|
518
|
-
const start = TimeGuard.from(
|
|
519
|
-
const end = TimeGuard.from(
|
|
518
|
+
const start = TimeGuard.from('2024-02-15'); // Enero
|
|
519
|
+
const end = TimeGuard.from('2024-04-15'); // Abril
|
|
520
520
|
const duration = start.until(end);
|
|
521
521
|
|
|
522
522
|
const explanation = duration.explain();
|
|
@@ -547,18 +547,18 @@ explanation.steps.forEach((step, i) => {
|
|
|
547
547
|
| `metadata?` | `Object` | Rendimiento y precisión |
|
|
548
548
|
|
|
549
549
|
```typescript
|
|
550
|
-
const date = TimeGuard.from(
|
|
550
|
+
const date = TimeGuard.from('2024-03-13 14:35:47.654');
|
|
551
551
|
|
|
552
552
|
// Redondear a diferentes unidades
|
|
553
|
-
date.round({ smallestUnit:
|
|
554
|
-
date.round({ smallestUnit:
|
|
555
|
-
date.round({ smallestUnit:
|
|
556
|
-
date.round({ smallestUnit:
|
|
553
|
+
date.round({ smallestUnit: 'second' }); // 2024-03-13 14:35:48
|
|
554
|
+
date.round({ smallestUnit: 'minute' }); // 2024-03-13 14:36:00
|
|
555
|
+
date.round({ smallestUnit: 'hour' }); // 2024-03-13 15:00:00
|
|
556
|
+
date.round({ smallestUnit: 'day' }); // 2024-03-14 00:00:00
|
|
557
557
|
|
|
558
558
|
// Modos de redondeo: 'ceil', 'floor', 'trunc', 'half' (predeterminado)
|
|
559
559
|
date.round({
|
|
560
|
-
smallestUnit:
|
|
561
|
-
roundingMode:
|
|
560
|
+
smallestUnit: 'minute',
|
|
561
|
+
roundingMode: 'ceil',
|
|
562
562
|
});
|
|
563
563
|
```
|
|
564
564
|
|
|
@@ -571,14 +571,14 @@ TimeGuard incluye soporte para múltiples sistemas de calendario, extensible a t
|
|
|
571
571
|
### Calendarios Soportados
|
|
572
572
|
|
|
573
573
|
```typescript
|
|
574
|
-
import { TimeGuard, CalendarManager } from
|
|
574
|
+
import { TimeGuard, CalendarManager } from '@bereasoftware/time-guard';
|
|
575
575
|
import {
|
|
576
576
|
IslamicCalendar,
|
|
577
577
|
HebrewCalendar,
|
|
578
578
|
ChineseCalendar,
|
|
579
579
|
JapaneseCalendar,
|
|
580
580
|
BuddhistCalendar,
|
|
581
|
-
} from
|
|
581
|
+
} from '@bereasoftware/time-guard/calendars';
|
|
582
582
|
|
|
583
583
|
// Get calendar manager
|
|
584
584
|
const calendarMgr = CalendarManager.getInstance();
|
|
@@ -592,7 +592,7 @@ const islamic = new IslamicCalendar();
|
|
|
592
592
|
calendarMgr.register(islamic);
|
|
593
593
|
|
|
594
594
|
// Get calendar info
|
|
595
|
-
const gregorian = calendarMgr.get(
|
|
595
|
+
const gregorian = calendarMgr.get('gregory');
|
|
596
596
|
console.log(gregorian.getMonthName(3)); // "March"
|
|
597
597
|
console.log(gregorian.getMonthName(3, true)); // "Mar"
|
|
598
598
|
console.log(gregorian.getWeekdayName(1)); // "Sunday"
|
|
@@ -604,7 +604,7 @@ console.log(gregorian.isLeapYear(2024)); // true
|
|
|
604
604
|
#### Calendario Gregoriano
|
|
605
605
|
|
|
606
606
|
```typescript
|
|
607
|
-
import { GregorianCalendar } from
|
|
607
|
+
import { GregorianCalendar } from '@bereasoftware/time-guard/calendars';
|
|
608
608
|
|
|
609
609
|
const calendar = new GregorianCalendar();
|
|
610
610
|
console.log(calendar.daysInMonth(2024, 2)); // 29 (leap year)
|
|
@@ -614,7 +614,7 @@ console.log(calendar.daysInYear(2024)); // 366
|
|
|
614
614
|
#### Calendario Islámico (Hijri)
|
|
615
615
|
|
|
616
616
|
```typescript
|
|
617
|
-
import { IslamicCalendar } from
|
|
617
|
+
import { IslamicCalendar } from '@bereasoftware/time-guard/calendars';
|
|
618
618
|
|
|
619
619
|
const calendar = new IslamicCalendar();
|
|
620
620
|
console.log(calendar.getMonthName(9)); // "Ramadan"
|
|
@@ -624,7 +624,7 @@ console.log(calendar.isLeapYear(1445)); // true
|
|
|
624
624
|
#### Calendario Hebreo
|
|
625
625
|
|
|
626
626
|
```typescript
|
|
627
|
-
import { HebrewCalendar } from
|
|
627
|
+
import { HebrewCalendar } from '@bereasoftware/time-guard/calendars';
|
|
628
628
|
|
|
629
629
|
const calendar = new HebrewCalendar();
|
|
630
630
|
console.log(calendar.getMonthName(1)); // "Tishrei"
|
|
@@ -634,7 +634,7 @@ console.log(calendar.isLeapYear(5784)); // true
|
|
|
634
634
|
#### Calendario Chino
|
|
635
635
|
|
|
636
636
|
```typescript
|
|
637
|
-
import { ChineseCalendar } from
|
|
637
|
+
import { ChineseCalendar } from '@bereasoftware/time-guard/calendars';
|
|
638
638
|
|
|
639
639
|
const calendar = new ChineseCalendar();
|
|
640
640
|
const zodiac = calendar.getZodiacSign(2024); // "龙" (Dragon)
|
|
@@ -643,7 +643,7 @@ const zodiac = calendar.getZodiacSign(2024); // "龙" (Dragon)
|
|
|
643
643
|
#### Calendario Japonés
|
|
644
644
|
|
|
645
645
|
```typescript
|
|
646
|
-
import { JapaneseCalendar } from
|
|
646
|
+
import { JapaneseCalendar } from '@bereasoftware/time-guard/calendars';
|
|
647
647
|
|
|
648
648
|
const calendar = new JapaneseCalendar();
|
|
649
649
|
console.log(calendar.getMonthName(3)); // "3月"
|
|
@@ -652,7 +652,7 @@ console.log(calendar.getMonthName(3)); // "3月"
|
|
|
652
652
|
#### Calendario Budista
|
|
653
653
|
|
|
654
654
|
```typescript
|
|
655
|
-
import { BuddhistCalendar } from
|
|
655
|
+
import { BuddhistCalendar } from '@bereasoftware/time-guard/calendars';
|
|
656
656
|
|
|
657
657
|
const calendar = new BuddhistCalendar();
|
|
658
658
|
// Year 2567 BE = 2024 CE
|
|
@@ -670,21 +670,21 @@ TimeGuard incluye un sistema de plugins opcional para funcionalidad extendida:
|
|
|
670
670
|
1. **Plugin de Tiempo Relativo** - Diferencias de tiempo legibles para humanos
|
|
671
671
|
|
|
672
672
|
```typescript
|
|
673
|
-
import { TimeGuard, PluginManager } from
|
|
674
|
-
import relativeTimePlugin from
|
|
673
|
+
import { TimeGuard, PluginManager } from '@bereasoftware/time-guard';
|
|
674
|
+
import relativeTimePlugin from '@bereasoftware/time-guard/plugins/relative-time';
|
|
675
675
|
|
|
676
676
|
PluginManager.use(relativeTimePlugin, TimeGuard);
|
|
677
677
|
|
|
678
|
-
TimeGuard.from(
|
|
679
|
-
TimeGuard.from(
|
|
678
|
+
TimeGuard.from('2024-01-01').fromNow(); // "2 months ago"
|
|
679
|
+
TimeGuard.from('2024-04-01').toNow(); // "in 19 days"
|
|
680
680
|
```
|
|
681
681
|
|
|
682
682
|
2. **Plugin de Duración** - Soporte de duración ISO 8601
|
|
683
683
|
|
|
684
684
|
```typescript
|
|
685
|
-
import { Duration } from
|
|
685
|
+
import { Duration } from '@bereasoftware/time-guard/plugins/duration';
|
|
686
686
|
|
|
687
|
-
const duration = Duration.fromISO(
|
|
687
|
+
const duration = Duration.fromISO('P2Y3M4D');
|
|
688
688
|
duration.humanize(); // "2 years, 3 months, 4 days"
|
|
689
689
|
duration.asDays(); // 1159
|
|
690
690
|
```
|
|
@@ -692,12 +692,12 @@ TimeGuard incluye un sistema de plugins opcional para funcionalidad extendida:
|
|
|
692
692
|
3. **Plugin de Formato Avanzado** - Tokens de formato extendidos
|
|
693
693
|
|
|
694
694
|
```typescript
|
|
695
|
-
import advancedFormatPlugin from
|
|
695
|
+
import advancedFormatPlugin from '@bereasoftware/time-guard/plugins/advanced-format';
|
|
696
696
|
|
|
697
697
|
PluginManager.use(advancedFormatPlugin, TimeGuard);
|
|
698
698
|
|
|
699
|
-
date.format(
|
|
700
|
-
date.format(
|
|
699
|
+
date.format('Do MMMM YYYY'); // "13th March 2024"
|
|
700
|
+
date.format('Q [Q] YYYY'); // "1 Q 2024"
|
|
701
701
|
```
|
|
702
702
|
|
|
703
703
|
**📖 Full Details:** See [PLUGINS.md](PLUGINS.md) for complete plugin documentation.
|
|
@@ -806,131 +806,131 @@ Los códigos de locale siguen el patrón estándar `[language]-[region]`:
|
|
|
806
806
|
#### Configuración de Locales
|
|
807
807
|
|
|
808
808
|
```typescript
|
|
809
|
-
import { TimeGuard } from
|
|
809
|
+
import { TimeGuard } from '@bereasoftware/time-guard';
|
|
810
810
|
|
|
811
|
-
const date = TimeGuard.from(
|
|
811
|
+
const date = TimeGuard.from('2024-03-13 14:30:00');
|
|
812
812
|
|
|
813
813
|
// Get current locale
|
|
814
814
|
const currentLocale = date.locale(); // "en"
|
|
815
815
|
|
|
816
816
|
// Change locale (returns new instance)
|
|
817
|
-
const spanish = date.locale(
|
|
818
|
-
const french = date.locale(
|
|
819
|
-
const japanese = date.locale(
|
|
820
|
-
const arabic = date.locale(
|
|
817
|
+
const spanish = date.locale('es');
|
|
818
|
+
const french = date.locale('fr');
|
|
819
|
+
const japanese = date.locale('ja');
|
|
820
|
+
const arabic = date.locale('ar');
|
|
821
821
|
|
|
822
822
|
// Chain operations
|
|
823
|
-
date.locale(
|
|
823
|
+
date.locale('es').format('dddd, DD MMMM YYYY'); // miércoles, 13 marzo 2024
|
|
824
824
|
|
|
825
825
|
// Or use constructor config
|
|
826
|
-
TimeGuard.from(
|
|
827
|
-
TimeGuard.now({ locale:
|
|
826
|
+
TimeGuard.from('2024-03-13', { locale: 'de' });
|
|
827
|
+
TimeGuard.now({ locale: 'ja' });
|
|
828
828
|
```
|
|
829
829
|
|
|
830
830
|
#### Formateo en Diferentes Locales
|
|
831
831
|
|
|
832
832
|
```typescript
|
|
833
|
-
const date = TimeGuard.from(
|
|
833
|
+
const date = TimeGuard.from('2024-03-13');
|
|
834
834
|
|
|
835
835
|
// English variants
|
|
836
|
-
date.locale(
|
|
837
|
-
date.locale(
|
|
838
|
-
date.locale(
|
|
839
|
-
date.locale(
|
|
836
|
+
date.locale('en').format('MMMM DD, YYYY'); // March 13, 2024
|
|
837
|
+
date.locale('en-gb').format('DD MMMM YYYY'); // 13 March 2024
|
|
838
|
+
date.locale('en-au').format('DD/MM/YYYY'); // 13/03/2024
|
|
839
|
+
date.locale('en-ca').format('YYYY-MM-DD'); // 2024-03-13
|
|
840
840
|
|
|
841
841
|
// Spanish variants
|
|
842
|
-
date.locale(
|
|
843
|
-
date.locale(
|
|
844
|
-
date.locale(
|
|
842
|
+
date.locale('es').format('DD MMMM YYYY'); // 13 marzo 2024
|
|
843
|
+
date.locale('es-mx').format('DD/MM/YYYY'); // 13/03/2024
|
|
844
|
+
date.locale('es-us').format('MMMM D'); // marzo 13
|
|
845
845
|
|
|
846
846
|
// Romance languages
|
|
847
|
-
date.locale(
|
|
848
|
-
date.locale(
|
|
849
|
-
date.locale(
|
|
850
|
-
date.locale(
|
|
851
|
-
date.locale(
|
|
847
|
+
date.locale('fr').format('dddd D MMMM YYYY'); // mercredi 13 mars 2024
|
|
848
|
+
date.locale('it').format('dddd, D MMMM YYYY'); // mercoledì, 13 marzo 2024
|
|
849
|
+
date.locale('pt').format('dddd, D MMMM YYYY'); // quarta-feira, 13 de março de 2024
|
|
850
|
+
date.locale('pt-br').format('DD/MM/YYYY'); // 13/03/2024
|
|
851
|
+
date.locale('ro').format('DD MMMM YYYY'); // 13 martie 2024
|
|
852
852
|
|
|
853
853
|
// Slavic languages
|
|
854
|
-
date.locale(
|
|
855
|
-
date.locale(
|
|
856
|
-
date.locale(
|
|
857
|
-
date.locale(
|
|
854
|
+
date.locale('ru').format('DD MMMM YYYY'); // 13 марта 2024
|
|
855
|
+
date.locale('pl').format('DD MMMM YYYY'); // 13 marca 2024
|
|
856
|
+
date.locale('cs').format('DD. MMMM YYYY'); // 13. března 2024
|
|
857
|
+
date.locale('sk').format('DD. MMMM YYYY'); // 13. marca 2024
|
|
858
858
|
|
|
859
859
|
// Nordic languages
|
|
860
|
-
date.locale(
|
|
861
|
-
date.locale(
|
|
862
|
-
date.locale(
|
|
863
|
-
date.locale(
|
|
860
|
+
date.locale('sv').format('DD MMMM YYYY'); // 13 mars 2024
|
|
861
|
+
date.locale('nb').format('DD. MMMM YYYY'); // 13. mars 2024
|
|
862
|
+
date.locale('da').format('DD. MMMM YYYY'); // 13. marts 2024
|
|
863
|
+
date.locale('fi').format('DD. MMMM YYYY'); // 13. maaliskuuta 2024
|
|
864
864
|
|
|
865
865
|
// Asian languages
|
|
866
|
-
date.locale(
|
|
867
|
-
date.locale(
|
|
868
|
-
date.locale(
|
|
869
|
-
date.locale(
|
|
870
|
-
date.locale(
|
|
871
|
-
date.locale(
|
|
872
|
-
date.locale(
|
|
866
|
+
date.locale('ja').format('YYYY年M月D日'); // 2024年3月13日
|
|
867
|
+
date.locale('zh-cn').format('YYYY年M月D日'); // 2024年3月13日
|
|
868
|
+
date.locale('zh-tw').format('YYYY年M月D日'); // 2024年3月13日
|
|
869
|
+
date.locale('ko').format('YYYY년 M월 D일'); // 2024년 3월 13일
|
|
870
|
+
date.locale('th').format('DD MMMM YYYY'); // 13 มีนาคม 2567 (BE)
|
|
871
|
+
date.locale('vi').format('DD/MM/YYYY'); // 13/03/2024
|
|
872
|
+
date.locale('id').format('DD MMMM YYYY'); // 13 Maret 2024
|
|
873
873
|
|
|
874
874
|
// European languages
|
|
875
|
-
date.locale(
|
|
876
|
-
date.locale(
|
|
877
|
-
date.locale(
|
|
878
|
-
date.locale(
|
|
879
|
-
date.locale(
|
|
880
|
-
date.locale(
|
|
881
|
-
date.locale(
|
|
875
|
+
date.locale('de').format('DD. MMMM YYYY'); // 13. März 2024
|
|
876
|
+
date.locale('nl').format('DD MMMM YYYY'); // 13 maart 2024
|
|
877
|
+
date.locale('el').format('DD MMMM YYYY'); // 13 Μαρτίου 2024
|
|
878
|
+
date.locale('hu').format('YYYY. MMMM DD.'); // 2024. március 13.
|
|
879
|
+
date.locale('eu').format('YYYY[ko] MMMM[ren] DD'); // 2024ko martsaren 13
|
|
880
|
+
date.locale('ca').format('DD MMMM YYYY'); // 13 de març de 2024
|
|
881
|
+
date.locale('tr').format('DD MMMM YYYY'); // 13 Mart 2024
|
|
882
882
|
|
|
883
883
|
// Middle Eastern & South Asian
|
|
884
|
-
date.locale(
|
|
885
|
-
date.locale(
|
|
886
|
-
date.locale(
|
|
884
|
+
date.locale('ar').format('DD MMMM YYYY'); // 13 مارس 2024
|
|
885
|
+
date.locale('he').format('DD.MM.YYYY'); // 13.03.2024
|
|
886
|
+
date.locale('hi').format('DD MMMM YYYY'); // 13 मार्च 2024
|
|
887
887
|
```
|
|
888
888
|
|
|
889
889
|
#### Nombres de Día y Mes
|
|
890
890
|
|
|
891
891
|
```typescript
|
|
892
892
|
// Get localized day names
|
|
893
|
-
date.locale(
|
|
894
|
-
date.locale(
|
|
895
|
-
date.locale(
|
|
896
|
-
date.locale(
|
|
897
|
-
date.locale(
|
|
893
|
+
date.locale('es').format('dddd'); // miércoles
|
|
894
|
+
date.locale('es').format('ddd'); // mié
|
|
895
|
+
date.locale('fr').format('dddd'); // mercredi
|
|
896
|
+
date.locale('de').format('dddd'); // Mittwoch
|
|
897
|
+
date.locale('ja').format('dddd'); // 水曜日
|
|
898
898
|
|
|
899
899
|
// Get localized month names
|
|
900
|
-
date.locale(
|
|
901
|
-
date.locale(
|
|
902
|
-
date.locale(
|
|
903
|
-
date.locale(
|
|
904
|
-
date.locale(
|
|
900
|
+
date.locale('es').format('MMMM'); // marzo
|
|
901
|
+
date.locale('es').format('MMM'); // mar
|
|
902
|
+
date.locale('fr').format('MMMM'); // mars
|
|
903
|
+
date.locale('de').format('MMMM'); // März
|
|
904
|
+
date.locale('ru').format('MMMM'); // марта
|
|
905
905
|
```
|
|
906
906
|
|
|
907
907
|
#### Aplicaciones Multilocale
|
|
908
908
|
|
|
909
909
|
```typescript
|
|
910
910
|
// Switch language at runtime (user preference)
|
|
911
|
-
let currentLocale =
|
|
911
|
+
let currentLocale = 'en';
|
|
912
912
|
|
|
913
913
|
function formatUserDate(
|
|
914
914
|
date: TimeGuard,
|
|
915
915
|
locale: string = currentLocale,
|
|
916
916
|
): string {
|
|
917
|
-
return date.locale(locale).format(
|
|
917
|
+
return date.locale(locale).format('dddd, MMMM D, YYYY [at] HH:mm');
|
|
918
918
|
}
|
|
919
919
|
|
|
920
920
|
const date = TimeGuard.now();
|
|
921
921
|
|
|
922
922
|
// English user
|
|
923
|
-
console.log(formatUserDate(date,
|
|
923
|
+
console.log(formatUserDate(date, 'en')); // Wednesday, March 13, 2024 at 14:30
|
|
924
924
|
|
|
925
925
|
// Spanish user
|
|
926
|
-
currentLocale =
|
|
927
|
-
console.log(formatUserDate(date,
|
|
926
|
+
currentLocale = 'es';
|
|
927
|
+
console.log(formatUserDate(date, 'es')); // miércoles, 13 de marzo de 2024 a las 14:30
|
|
928
928
|
|
|
929
929
|
// French user
|
|
930
|
-
console.log(formatUserDate(date,
|
|
930
|
+
console.log(formatUserDate(date, 'fr')); // mercredi, 13 mars 2024 à 14:30
|
|
931
931
|
|
|
932
932
|
// Japanese user
|
|
933
|
-
console.log(formatUserDate(date,
|
|
933
|
+
console.log(formatUserDate(date, 'ja')); // 水曜日、2024年3月13日 14:30
|
|
934
934
|
```
|
|
935
935
|
|
|
936
936
|
#### Obtener Locales Disponibles Programáticamente
|
|
@@ -941,10 +941,10 @@ const locales = TimeGuard.getAvailableLocales();
|
|
|
941
941
|
// Returns: ['en', 'en-au', 'en-gb', 'en-ca', 'es', 'es-mx', 'es-us', ...]
|
|
942
942
|
|
|
943
943
|
// Filter by prefix
|
|
944
|
-
const englishLocales = locales.filter((l) => l.startsWith(
|
|
945
|
-
const spanishLocales = locales.filter((l) => l.startsWith(
|
|
944
|
+
const englishLocales = locales.filter((l) => l.startsWith('en'));
|
|
945
|
+
const spanishLocales = locales.filter((l) => l.startsWith('es'));
|
|
946
946
|
const asianLocales = locales.filter((l) =>
|
|
947
|
-
[
|
|
947
|
+
['ja', 'zh-cn', 'zh-tw', 'ko'].includes(l),
|
|
948
948
|
);
|
|
949
949
|
|
|
950
950
|
// Create locale selector UI
|
|
@@ -952,7 +952,7 @@ function createLocaleSelector() {
|
|
|
952
952
|
const locales = TimeGuard.getAvailableLocales();
|
|
953
953
|
return locales.map((locale) => ({
|
|
954
954
|
code: locale,
|
|
955
|
-
label: new Intl.DisplayNames(
|
|
955
|
+
label: new Intl.DisplayNames('en', { type: 'language' }).of(locale),
|
|
956
956
|
}));
|
|
957
957
|
}
|
|
958
958
|
```
|
|
@@ -968,7 +968,7 @@ TimeGuard includes a powerful plugin system for extending functionality. Plugins
|
|
|
968
968
|
### Plugin Manager
|
|
969
969
|
|
|
970
970
|
```typescript
|
|
971
|
-
import { TimeGuard, PluginManager } from
|
|
971
|
+
import { TimeGuard, PluginManager } from '@bereasoftware/time-guard';
|
|
972
972
|
|
|
973
973
|
// Use a plugin
|
|
974
974
|
PluginManager.use(myPlugin, TimeGuard);
|
|
@@ -985,14 +985,14 @@ PluginManager.listInstalled();
|
|
|
985
985
|
Añade diferencias de tiempo legibles como "hace 2 horas" o "en 3 días".
|
|
986
986
|
|
|
987
987
|
```typescript
|
|
988
|
-
import { TimeGuard, PluginManager } from
|
|
989
|
-
import relativeTimePlugin from
|
|
988
|
+
import { TimeGuard, PluginManager } from '@bereasoftware/time-guard';
|
|
989
|
+
import relativeTimePlugin from '@bereasoftware/time-guard/plugins/relative-time';
|
|
990
990
|
|
|
991
991
|
// Install plugin once
|
|
992
992
|
PluginManager.use(relativeTimePlugin, TimeGuard);
|
|
993
993
|
|
|
994
994
|
// Now use relative time methods
|
|
995
|
-
const date = TimeGuard.from(
|
|
995
|
+
const date = TimeGuard.from('2024-01-15');
|
|
996
996
|
|
|
997
997
|
// Relative to now
|
|
998
998
|
date.fromNow(); // "2 months ago"
|
|
@@ -1003,7 +1003,7 @@ date.fromNow(true); // "2 months"
|
|
|
1003
1003
|
date.toNow(true); // "2 months"
|
|
1004
1004
|
|
|
1005
1005
|
// Relative to another date
|
|
1006
|
-
const other = TimeGuard.from(
|
|
1006
|
+
const other = TimeGuard.from('2024-02-15');
|
|
1007
1007
|
date.from(other); // "a month ago"
|
|
1008
1008
|
date.to(other); // "in a month"
|
|
1009
1009
|
|
|
@@ -1036,12 +1036,12 @@ date.humanize(other, true); // "a month" (exact mode)
|
|
|
1036
1036
|
Soporte de duración ISO 8601 con cálculos avanzados.
|
|
1037
1037
|
|
|
1038
1038
|
```typescript
|
|
1039
|
-
import { TimeGuard } from
|
|
1039
|
+
import { TimeGuard } from '@bereasoftware/time-guard';
|
|
1040
1040
|
import {
|
|
1041
1041
|
Duration,
|
|
1042
1042
|
durationPlugin,
|
|
1043
|
-
} from
|
|
1044
|
-
import { PluginManager } from
|
|
1043
|
+
} from '@bereasoftware/time-guard/plugins/duration';
|
|
1044
|
+
import { PluginManager } from '@bereasoftware/time-guard';
|
|
1045
1045
|
|
|
1046
1046
|
// Install plugin
|
|
1047
1047
|
PluginManager.use(durationPlugin, TimeGuard);
|
|
@@ -1058,7 +1058,7 @@ const duration1 = new Duration({
|
|
|
1058
1058
|
});
|
|
1059
1059
|
|
|
1060
1060
|
// From ISO 8601 string
|
|
1061
|
-
const duration2 = Duration.fromISO(
|
|
1061
|
+
const duration2 = Duration.fromISO('P3Y6M4DT12H30M5S');
|
|
1062
1062
|
// P = Period marker
|
|
1063
1063
|
// 3Y = 3 years
|
|
1064
1064
|
// 6M = 6 months
|
|
@@ -1069,8 +1069,8 @@ const duration2 = Duration.fromISO("P3Y6M4DT12H30M5S");
|
|
|
1069
1069
|
// 5S = 5 seconds
|
|
1070
1070
|
|
|
1071
1071
|
// From TimeGuard dates
|
|
1072
|
-
const start = TimeGuard.from(
|
|
1073
|
-
const end = TimeGuard.from(
|
|
1072
|
+
const start = TimeGuard.from('2024-01-15');
|
|
1073
|
+
const end = TimeGuard.from('2024-05-20');
|
|
1074
1074
|
const between = Duration.between(start, end);
|
|
1075
1075
|
// { years: 0, months: 4, days: 5, ... }
|
|
1076
1076
|
|
|
@@ -1087,8 +1087,8 @@ duration1.asMilliseconds(); // Total milliseconds
|
|
|
1087
1087
|
|
|
1088
1088
|
// Humanize
|
|
1089
1089
|
duration1.humanize(); // "2 years, 3 months, 4 days, 12 hours, 30 minutes"
|
|
1090
|
-
duration1.humanize(
|
|
1091
|
-
duration1.humanize(
|
|
1090
|
+
duration1.humanize('es'); // Spanish: "2 años, 3 meses..."
|
|
1091
|
+
duration1.humanize('fr'); // French: "2 ans, 3 mois..."
|
|
1092
1092
|
|
|
1093
1093
|
// Get components
|
|
1094
1094
|
duration1.years;
|
|
@@ -1112,15 +1112,15 @@ duration1.negate(); // Reverse direction
|
|
|
1112
1112
|
**ISO 8601 Duration Examples:**
|
|
1113
1113
|
|
|
1114
1114
|
```typescript
|
|
1115
|
-
Duration.fromISO(
|
|
1116
|
-
Duration.fromISO(
|
|
1117
|
-
Duration.fromISO(
|
|
1118
|
-
Duration.fromISO(
|
|
1119
|
-
Duration.fromISO(
|
|
1120
|
-
Duration.fromISO(
|
|
1121
|
-
Duration.fromISO(
|
|
1122
|
-
Duration.fromISO(
|
|
1123
|
-
Duration.fromISO(
|
|
1115
|
+
Duration.fromISO('P1Y'); // 1 year
|
|
1116
|
+
Duration.fromISO('P3M'); // 3 months
|
|
1117
|
+
Duration.fromISO('P1W'); // 1 week (7 days)
|
|
1118
|
+
Duration.fromISO('P1D'); // 1 day
|
|
1119
|
+
Duration.fromISO('PT1H'); // 1 hour
|
|
1120
|
+
Duration.fromISO('PT30M'); // 30 minutes
|
|
1121
|
+
Duration.fromISO('PT45S'); // 45 seconds
|
|
1122
|
+
Duration.fromISO('P1Y2M3DT4H5M6S'); // Complex: 1 year, 2 months, ...
|
|
1123
|
+
Duration.fromISO('-P1D'); // Negative: -1 day
|
|
1124
1124
|
```
|
|
1125
1125
|
|
|
1126
1126
|
---
|
|
@@ -1130,31 +1130,31 @@ Duration.fromISO("-P1D"); // Negative: -1 day
|
|
|
1130
1130
|
Tokens de formato extendidos para necesidades de formateo especializadas.
|
|
1131
1131
|
|
|
1132
1132
|
```typescript
|
|
1133
|
-
import { TimeGuard, PluginManager } from
|
|
1134
|
-
import advancedFormatPlugin from
|
|
1133
|
+
import { TimeGuard, PluginManager } from '@bereasoftware/time-guard';
|
|
1134
|
+
import advancedFormatPlugin from '@bereasoftware/time-guard/plugins/advanced-format';
|
|
1135
1135
|
|
|
1136
1136
|
// Install plugin
|
|
1137
1137
|
PluginManager.use(advancedFormatPlugin, TimeGuard);
|
|
1138
1138
|
|
|
1139
|
-
const date = TimeGuard.from(
|
|
1139
|
+
const date = TimeGuard.from('2024-03-13 14:30:00');
|
|
1140
1140
|
|
|
1141
1141
|
// Advanced tokens become available
|
|
1142
|
-
date.format(
|
|
1143
|
-
date.format(
|
|
1144
|
-
date.format(
|
|
1145
|
-
date.format(
|
|
1146
|
-
date.format(
|
|
1147
|
-
date.format(
|
|
1142
|
+
date.format('Do MMMM YYYY'); // "13th March 2024"
|
|
1143
|
+
date.format('Q [Q] YYYY'); // "1 Q 2024"
|
|
1144
|
+
date.format('[Week] w, YYYY'); // "Week 11, 2024"
|
|
1145
|
+
date.format('W [of] ww'); // "11 of 11"
|
|
1146
|
+
date.format('gggg-[W]ww'); // "2024-W11" (ISO week)
|
|
1147
|
+
date.format('GGGG-[W]WW'); // 2024-W11 (alternative)
|
|
1148
1148
|
|
|
1149
1149
|
// Timezone abbreviation
|
|
1150
|
-
date.format(
|
|
1150
|
+
date.format('HH:mm zzz'); // "14:30 UTC"
|
|
1151
1151
|
|
|
1152
1152
|
// 24-hour (k = 1-24 instead of 0-23)
|
|
1153
|
-
date.format(
|
|
1153
|
+
date.format('k:mm'); // "14:30"
|
|
1154
1154
|
|
|
1155
1155
|
// Unix timestamps
|
|
1156
|
-
date.format(
|
|
1157
|
-
date.format(
|
|
1156
|
+
date.format('X'); // Unix seconds
|
|
1157
|
+
date.format('x'); // Unix milliseconds
|
|
1158
1158
|
```
|
|
1159
1159
|
|
|
1160
1160
|
**Advanced Format Tokens:**
|
|
@@ -1190,17 +1190,17 @@ interface ITimeGuardPlugin {
|
|
|
1190
1190
|
### Creating Custom Plugins
|
|
1191
1191
|
|
|
1192
1192
|
```typescript
|
|
1193
|
-
import { TimeGuard } from
|
|
1194
|
-
import type { ITimeGuardPlugin } from
|
|
1193
|
+
import { TimeGuard } from '@bereasoftware/time-guard';
|
|
1194
|
+
import type { ITimeGuardPlugin } from '@bereasoftware/time-guard/types';
|
|
1195
1195
|
|
|
1196
1196
|
class MyCustomPlugin implements ITimeGuardPlugin {
|
|
1197
|
-
name =
|
|
1198
|
-
version =
|
|
1197
|
+
name = 'my-plugin';
|
|
1198
|
+
version = '1.0.0';
|
|
1199
1199
|
|
|
1200
1200
|
install(TimeGuardClass: typeof TimeGuard): void {
|
|
1201
1201
|
// Add method to TimeGuard prototype
|
|
1202
1202
|
(TimeGuardClass.prototype as any).myMethod = function () {
|
|
1203
|
-
return
|
|
1203
|
+
return 'Hello from my plugin!';
|
|
1204
1204
|
};
|
|
1205
1205
|
}
|
|
1206
1206
|
}
|
|
@@ -1225,15 +1225,15 @@ date.myMethod(); // "Hello from my plugin!"
|
|
|
1225
1225
|
```typescript
|
|
1226
1226
|
// Create current date/time
|
|
1227
1227
|
TimeGuard.now();
|
|
1228
|
-
TimeGuard.now({ locale:
|
|
1228
|
+
TimeGuard.now({ locale: 'es', timezone: 'America/Mexico_City' });
|
|
1229
1229
|
|
|
1230
1230
|
// Create from various inputs
|
|
1231
|
-
TimeGuard.from(
|
|
1232
|
-
TimeGuard.from(
|
|
1231
|
+
TimeGuard.from('2024-03-13');
|
|
1232
|
+
TimeGuard.from('2024-03-13T14:30:00');
|
|
1233
1233
|
TimeGuard.from(new Date());
|
|
1234
1234
|
TimeGuard.from(1234567890000); // milliseconds
|
|
1235
|
-
TimeGuard.from(1234567890, { timezone:
|
|
1236
|
-
TimeGuard.from(
|
|
1235
|
+
TimeGuard.from(1234567890, { timezone: 'UTC' }); // seconds
|
|
1236
|
+
TimeGuard.from('2024-03-13', { locale: 'es' });
|
|
1237
1237
|
|
|
1238
1238
|
// Create from Temporal object
|
|
1239
1239
|
TimeGuard.fromTemporal(temporalPlainDateTime, config);
|
|
@@ -1242,7 +1242,7 @@ TimeGuard.fromTemporal(temporalPlainDateTime, config);
|
|
|
1242
1242
|
### 🔄 Conversion Methods
|
|
1243
1243
|
|
|
1244
1244
|
```typescript
|
|
1245
|
-
const date = TimeGuard.from(
|
|
1245
|
+
const date = TimeGuard.from('2024-03-13 14:30:45');
|
|
1246
1246
|
|
|
1247
1247
|
date.toDate(); // Convert to JavaScript Date
|
|
1248
1248
|
date.toTemporal(); // Get underlying Temporal object
|
|
@@ -1257,7 +1257,7 @@ date.unix(); // Seconds since epoch
|
|
|
1257
1257
|
### ➕ Manipulation Methods
|
|
1258
1258
|
|
|
1259
1259
|
```typescript
|
|
1260
|
-
const date = TimeGuard.from(
|
|
1260
|
+
const date = TimeGuard.from('2024-03-13 14:30:00');
|
|
1261
1261
|
|
|
1262
1262
|
// Add time - accepts partial record of units
|
|
1263
1263
|
date.add({ days: 5 });
|
|
@@ -1274,12 +1274,12 @@ date.set({ hour: 10, minute: 0 });
|
|
|
1274
1274
|
date.set({ year: 2025, month: 1, day: 1 });
|
|
1275
1275
|
|
|
1276
1276
|
// Start/End of period
|
|
1277
|
-
date.startOf(
|
|
1278
|
-
date.startOf(
|
|
1279
|
-
date.startOf(
|
|
1280
|
-
date.startOf(
|
|
1281
|
-
date.endOf(
|
|
1282
|
-
date.endOf(
|
|
1277
|
+
date.startOf('year'); // 2024-01-01 00:00:00
|
|
1278
|
+
date.startOf('month'); // 2024-03-01 00:00:00
|
|
1279
|
+
date.startOf('day'); // 2024-03-13 00:00:00
|
|
1280
|
+
date.startOf('hour'); // 2024-03-13 14:00:00
|
|
1281
|
+
date.endOf('year'); // 2024-12-31 23:59:59
|
|
1282
|
+
date.endOf('month'); // 2024-03-31 23:59:59
|
|
1283
1283
|
|
|
1284
1284
|
date.clone(); // Create independent copy
|
|
1285
1285
|
```
|
|
@@ -1287,7 +1287,7 @@ date.clone(); // Create independent copy
|
|
|
1287
1287
|
### 🔍 Component Accessors (Getters)
|
|
1288
1288
|
|
|
1289
1289
|
```typescript
|
|
1290
|
-
const date = TimeGuard.from(
|
|
1290
|
+
const date = TimeGuard.from('2024-03-13 14:30:45.123');
|
|
1291
1291
|
|
|
1292
1292
|
// Date components
|
|
1293
1293
|
date.year(); // 2024
|
|
@@ -1315,8 +1315,8 @@ date.inLeapYear(); // true
|
|
|
1315
1315
|
### ⚖️ Comparison Methods
|
|
1316
1316
|
|
|
1317
1317
|
```typescript
|
|
1318
|
-
const date1 = TimeGuard.from(
|
|
1319
|
-
const date2 = TimeGuard.from(
|
|
1318
|
+
const date1 = TimeGuard.from('2024-03-13');
|
|
1319
|
+
const date2 = TimeGuard.from('2024-03-20');
|
|
1320
1320
|
|
|
1321
1321
|
// Direct comparison
|
|
1322
1322
|
date1.isBefore(date2); // true
|
|
@@ -1324,99 +1324,99 @@ date1.isAfter(date2); // false
|
|
|
1324
1324
|
date1.isSame(date1); // true
|
|
1325
1325
|
|
|
1326
1326
|
// Unit-specific comparison
|
|
1327
|
-
date1.isSame(date2,
|
|
1328
|
-
date1.isSame(date2,
|
|
1329
|
-
date1.isSame(date2,
|
|
1327
|
+
date1.isSame(date2, 'month'); // true (same month)
|
|
1328
|
+
date1.isSame(date2, 'year'); // true (same year)
|
|
1329
|
+
date1.isSame(date2, 'day'); // false (different day)
|
|
1330
1330
|
|
|
1331
1331
|
// Range checking
|
|
1332
1332
|
date1.isBetween(date1, date2); // true
|
|
1333
|
-
date1.isBetween(date1, date2, undefined,
|
|
1334
|
-
date1.isBetween(date1, date2, undefined,
|
|
1335
|
-
date1.isBetween(date1, date2,
|
|
1333
|
+
date1.isBetween(date1, date2, undefined, '[]'); // inclusive both ends
|
|
1334
|
+
date1.isBetween(date1, date2, undefined, '()'); // exclusive both ends
|
|
1335
|
+
date1.isBetween(date1, date2, 'month', '[]'); // granular range
|
|
1336
1336
|
|
|
1337
1337
|
// Calculate difference - Traditional API (backward compatible)
|
|
1338
|
-
date1.diff(date2,
|
|
1339
|
-
date1.diff(date2,
|
|
1340
|
-
date1.diff(date2,
|
|
1338
|
+
date1.diff(date2, 'days'); // -7
|
|
1339
|
+
date1.diff(date2, 'millisecond'); // difference in ms
|
|
1340
|
+
date1.diff(date2, 'months'); // -0
|
|
1341
1341
|
|
|
1342
1342
|
// Calculate difference - Modern Calendar-Aware API
|
|
1343
|
-
const start = TimeGuard.from(
|
|
1344
|
-
const end = TimeGuard.from(
|
|
1343
|
+
const start = TimeGuard.from('2024-01-15');
|
|
1344
|
+
const end = TimeGuard.from('2024-03-20');
|
|
1345
1345
|
|
|
1346
1346
|
// Exact mode (default) - precise time difference
|
|
1347
|
-
const exactDiff = start.diff(end, { mode:
|
|
1348
|
-
console.log(exactDiff.as(
|
|
1347
|
+
const exactDiff = start.diff(end, { mode: 'exact' });
|
|
1348
|
+
console.log(exactDiff.as('day')); // 65 days
|
|
1349
1349
|
|
|
1350
1350
|
// Calendar mode - human-readable breakdown
|
|
1351
|
-
const calendarDiff = start.diff(end, { mode:
|
|
1352
|
-
console.log(calendarDiff.format(
|
|
1353
|
-
console.log(calendarDiff.format(
|
|
1351
|
+
const calendarDiff = start.diff(end, { mode: 'calendar' });
|
|
1352
|
+
console.log(calendarDiff.format('en')); // "2 months and 5 days"
|
|
1353
|
+
console.log(calendarDiff.format('es')); // "2 meses y 5 días"
|
|
1354
1354
|
console.log(calendarDiff.breakdown()); // { years: 0, months: 2, weeks: 0, days: 5, ... }
|
|
1355
1355
|
|
|
1356
1356
|
// Fluent API for unit conversion
|
|
1357
1357
|
const diff = start.diff(end);
|
|
1358
|
-
console.log(diff.as(
|
|
1359
|
-
console.log(diff.as(
|
|
1358
|
+
console.log(diff.as('day')); // 65
|
|
1359
|
+
console.log(diff.as('hour')); // 1560
|
|
1360
1360
|
console.log(diff.valueOf()); // milliseconds
|
|
1361
1361
|
```
|
|
1362
1362
|
|
|
1363
1363
|
### 📊 Advanced Calculations
|
|
1364
1364
|
|
|
1365
1365
|
```typescript
|
|
1366
|
-
const date = TimeGuard.from(
|
|
1367
|
-
const future = TimeGuard.from(
|
|
1366
|
+
const date = TimeGuard.from('2024-01-15');
|
|
1367
|
+
const future = TimeGuard.from('2024-05-20');
|
|
1368
1368
|
|
|
1369
1369
|
// Duration: Get complete breakdown
|
|
1370
1370
|
const duration = date.until(future);
|
|
1371
1371
|
// { years: 0, months: 4, days: 5, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }
|
|
1372
1372
|
|
|
1373
1373
|
// Rounding: Precision control
|
|
1374
|
-
date.round({ smallestUnit:
|
|
1375
|
-
date.round({ smallestUnit:
|
|
1376
|
-
date.round({ smallestUnit:
|
|
1377
|
-
date.round({ smallestUnit:
|
|
1378
|
-
date.round({ smallestUnit:
|
|
1374
|
+
date.round({ smallestUnit: 'millisecond' }); // Default, no change
|
|
1375
|
+
date.round({ smallestUnit: 'second' }); // Removes milliseconds
|
|
1376
|
+
date.round({ smallestUnit: 'minute' }); // 2024-03-13 14:30:00
|
|
1377
|
+
date.round({ smallestUnit: 'hour' }); // 2024-03-13 14:00:00
|
|
1378
|
+
date.round({ smallestUnit: 'day' }); // 2024-03-13 00:00:00
|
|
1379
1379
|
|
|
1380
1380
|
// Rounding modes
|
|
1381
1381
|
date.round({
|
|
1382
|
-
smallestUnit:
|
|
1383
|
-
roundingMode:
|
|
1382
|
+
smallestUnit: 'minute',
|
|
1383
|
+
roundingMode: 'halfExpand', // Default: round to nearest
|
|
1384
1384
|
});
|
|
1385
1385
|
|
|
1386
1386
|
date.round({
|
|
1387
|
-
smallestUnit:
|
|
1388
|
-
roundingMode:
|
|
1387
|
+
smallestUnit: 'minute',
|
|
1388
|
+
roundingMode: 'ceil', // Always round up
|
|
1389
1389
|
});
|
|
1390
1390
|
```
|
|
1391
1391
|
|
|
1392
1392
|
### 🌍 Locale & Timezone
|
|
1393
1393
|
|
|
1394
1394
|
```typescript
|
|
1395
|
-
const date = TimeGuard.from(
|
|
1395
|
+
const date = TimeGuard.from('2024-03-13 14:30:00');
|
|
1396
1396
|
|
|
1397
1397
|
// Get/Set locale
|
|
1398
1398
|
date.locale(); // Returns current locale: 'en'
|
|
1399
|
-
date.locale(
|
|
1399
|
+
date.locale('es'); // Set locale, returns new instance
|
|
1400
1400
|
|
|
1401
1401
|
// Format in different locales
|
|
1402
|
-
date.format(
|
|
1403
|
-
date.locale(
|
|
1404
|
-
date.locale(
|
|
1405
|
-
date.locale(
|
|
1406
|
-
date.locale(
|
|
1407
|
-
date.locale(
|
|
1408
|
-
date.locale(
|
|
1409
|
-
date.locale(
|
|
1402
|
+
date.format('YYYY-MM-DD'); // 2024-03-13
|
|
1403
|
+
date.locale('es').format('DD MMMM YYYY'); // 13 marzo 2024
|
|
1404
|
+
date.locale('es-mx').format('DD/MM/YYYY'); // 13/03/2024
|
|
1405
|
+
date.locale('fr').format('dddd, DD MMMM YYYY'); // mercredi, 13 mars 2024
|
|
1406
|
+
date.locale('de').format('DD. MMMM YYYY'); // 13. März 2024
|
|
1407
|
+
date.locale('ja').format('YYYY年M月D日'); // 2024年3月13日
|
|
1408
|
+
date.locale('zh-cn').format('YYYY年M月D日'); // 2024年3月13日
|
|
1409
|
+
date.locale('ar').format('DD MMMM YYYY'); // 13 مارس 2024
|
|
1410
1410
|
|
|
1411
1411
|
// Get/Set timezone
|
|
1412
1412
|
date.timezone(); // Returns current timezone: 'UTC'
|
|
1413
|
-
const inNYC = date.timezone(
|
|
1414
|
-
const inTokyo = date.timezone(
|
|
1415
|
-
const inDubai = date.timezone(
|
|
1413
|
+
const inNYC = date.timezone('America/New_York');
|
|
1414
|
+
const inTokyo = date.timezone('Asia/Tokyo');
|
|
1415
|
+
const inDubai = date.timezone('Asia/Dubai');
|
|
1416
1416
|
|
|
1417
1417
|
// Format with timezone info
|
|
1418
|
-
inNYC.format(
|
|
1419
|
-
inTokyo.format(
|
|
1418
|
+
inNYC.format('YYYY-MM-DD HH:mm:ss Z'); // 2024-03-13 10:30:00 -04:00
|
|
1419
|
+
inTokyo.format('YYYY-MM-DD HH:mm:ss Z'); // 2024-03-13 23:30:00 +09:00
|
|
1420
1420
|
|
|
1421
1421
|
// Get all available locales
|
|
1422
1422
|
TimeGuard.getAvailableLocales(); // Array of 40+ locale codes
|
|
@@ -1427,16 +1427,16 @@ TimeGuard.getAvailableLocales(); // Array of 40+ locale codes
|
|
|
1427
1427
|
#### Format Presets
|
|
1428
1428
|
|
|
1429
1429
|
```typescript
|
|
1430
|
-
const date = TimeGuard.from(
|
|
1430
|
+
const date = TimeGuard.from('2024-03-13 14:30:45.123');
|
|
1431
1431
|
|
|
1432
1432
|
// Built-in presets
|
|
1433
|
-
date.format(
|
|
1434
|
-
date.format(
|
|
1435
|
-
date.format(
|
|
1436
|
-
date.format(
|
|
1437
|
-
date.format(
|
|
1438
|
-
date.format(
|
|
1439
|
-
date.format(
|
|
1433
|
+
date.format('iso'); // 2024-03-13T14:30:45.123Z
|
|
1434
|
+
date.format('date'); // 2024-03-13
|
|
1435
|
+
date.format('time'); // 14:30:45
|
|
1436
|
+
date.format('datetime'); // 2024-03-13 14:30:45
|
|
1437
|
+
date.format('rfc2822'); // Wed, 13 Mar 2024 14:30:45 +0000
|
|
1438
|
+
date.format('rfc3339'); // 2024-03-13T14:30:45Z
|
|
1439
|
+
date.format('utc'); // 2024-03-13T14:30:45.123Z
|
|
1440
1440
|
```
|
|
1441
1441
|
|
|
1442
1442
|
#### Format Tokens
|
|
@@ -1485,15 +1485,15 @@ date.format("utc"); // 2024-03-13T14:30:45.123Z
|
|
|
1485
1485
|
#### Custom Format Examples
|
|
1486
1486
|
|
|
1487
1487
|
```typescript
|
|
1488
|
-
date.format(
|
|
1489
|
-
date.format(
|
|
1490
|
-
date.format(
|
|
1491
|
-
date.format(
|
|
1492
|
-
date.format(
|
|
1493
|
-
date.format(
|
|
1494
|
-
date.format(
|
|
1495
|
-
date.format(
|
|
1496
|
-
date.format(
|
|
1488
|
+
date.format('YYYY-MM-DD'); // 2024-03-13
|
|
1489
|
+
date.format('DD/MM/YYYY'); // 13/03/2024
|
|
1490
|
+
date.format('MMMM D, YYYY'); // March 13, 2024
|
|
1491
|
+
date.format('dddd, MMMM D, YYYY'); // Wednesday, March 13, 2024
|
|
1492
|
+
date.format('DD-MMMM-YY'); // 13-Mar-24
|
|
1493
|
+
date.format('h:mm A'); // 2:30 PM
|
|
1494
|
+
date.format('HH:mm:ss'); // 14:30:45
|
|
1495
|
+
date.format('[Today is] dddd'); // Today is Wednesday
|
|
1496
|
+
date.format('HH:mm [UTC]'); // 14:30 UTC
|
|
1497
1497
|
date.format('DD/MM/YYYY "at" HH:mm'); // 13/03/2024 at 14:30
|
|
1498
1498
|
```
|
|
1499
1499
|
|
|
@@ -1507,8 +1507,8 @@ One of TimeGuard's most powerful features is its **calendar-aware diff mode**, w
|
|
|
1507
1507
|
|
|
1508
1508
|
```typescript
|
|
1509
1509
|
// Problem: How much time between Jan 15 and Mar 20?
|
|
1510
|
-
const start = TimeGuard.from(
|
|
1511
|
-
const end = TimeGuard.from(
|
|
1510
|
+
const start = TimeGuard.from('2024-01-15');
|
|
1511
|
+
const end = TimeGuard.from('2024-03-20');
|
|
1512
1512
|
|
|
1513
1513
|
// ❌ Naive approach: Just count days
|
|
1514
1514
|
// Result: "65 days" (technically correct but not user-friendly)
|
|
@@ -1520,22 +1520,22 @@ const end = TimeGuard.from("2024-03-20");
|
|
|
1520
1520
|
#### API Modes
|
|
1521
1521
|
|
|
1522
1522
|
```typescript
|
|
1523
|
-
const start = TimeGuard.from(
|
|
1524
|
-
const end = TimeGuard.from(
|
|
1523
|
+
const start = TimeGuard.from('2024-01-15');
|
|
1524
|
+
const end = TimeGuard.from('2024-03-20');
|
|
1525
1525
|
|
|
1526
1526
|
// ==== EXACT MODE (default) ====
|
|
1527
1527
|
// Precise time difference in specified unit
|
|
1528
|
-
const exactDiff = start.diff(end, { mode:
|
|
1528
|
+
const exactDiff = start.diff(end, { mode: 'exact' });
|
|
1529
1529
|
|
|
1530
1530
|
// Returns normalized difference
|
|
1531
|
-
exactDiff.as(
|
|
1532
|
-
exactDiff.as(
|
|
1533
|
-
exactDiff.as(
|
|
1531
|
+
exactDiff.as('day'); // 65 days
|
|
1532
|
+
exactDiff.as('hour'); // 1560 hours
|
|
1533
|
+
exactDiff.as('week'); // 9 weeks
|
|
1534
1534
|
exactDiff.valueOf(); // milliseconds
|
|
1535
1535
|
|
|
1536
1536
|
// ==== CALENDAR MODE ====
|
|
1537
1537
|
// Human-readable breakdown with automatic localization
|
|
1538
|
-
const calendarDiff = start.diff(end, { mode:
|
|
1538
|
+
const calendarDiff = start.diff(end, { mode: 'calendar' });
|
|
1539
1539
|
|
|
1540
1540
|
// Get structured breakdown
|
|
1541
1541
|
calendarDiff.breakdown();
|
|
@@ -1551,41 +1551,41 @@ calendarDiff.breakdown();
|
|
|
1551
1551
|
// }
|
|
1552
1552
|
|
|
1553
1553
|
// Format in any locale
|
|
1554
|
-
calendarDiff.format(
|
|
1555
|
-
calendarDiff.format(
|
|
1556
|
-
calendarDiff.format(
|
|
1557
|
-
calendarDiff.format(
|
|
1554
|
+
calendarDiff.format('en'); // "2 months and 5 days"
|
|
1555
|
+
calendarDiff.format('es'); // "2 meses y 5 días"
|
|
1556
|
+
calendarDiff.format('fr'); // "2 mois et 5 jours"
|
|
1557
|
+
calendarDiff.format('de'); // "2 Monate und 5 Tage"
|
|
1558
1558
|
```
|
|
1559
1559
|
|
|
1560
1560
|
#### Backward Compatibility
|
|
1561
1561
|
|
|
1562
1562
|
```typescript
|
|
1563
1563
|
// Traditional API still works perfectly
|
|
1564
|
-
const tg1 = TimeGuard.from(
|
|
1565
|
-
const tg2 = TimeGuard.from(
|
|
1564
|
+
const tg1 = TimeGuard.from('2024-01-15');
|
|
1565
|
+
const tg2 = TimeGuard.from('2024-03-20');
|
|
1566
1566
|
|
|
1567
|
-
tg1.diff(tg2,
|
|
1568
|
-
tg1.diff(tg2,
|
|
1569
|
-
tg1.diff(tg2,
|
|
1567
|
+
tg1.diff(tg2, 'day'); // 65 (returns number)
|
|
1568
|
+
tg1.diff(tg2, 'month'); // 2
|
|
1569
|
+
tg1.diff(tg2, 'millisecond'); // raw milliseconds
|
|
1570
1570
|
```
|
|
1571
1571
|
|
|
1572
1572
|
#### Advanced Usage
|
|
1573
1573
|
|
|
1574
1574
|
```typescript
|
|
1575
|
-
const start = TimeGuard.from(
|
|
1576
|
-
const end = TimeGuard.from(
|
|
1575
|
+
const start = TimeGuard.from('2024-01-15 10:30:00');
|
|
1576
|
+
const end = TimeGuard.from('2024-03-20 15:45:00');
|
|
1577
1577
|
|
|
1578
1578
|
// Fluent API for unit conversion
|
|
1579
1579
|
const diff = start.diff(end);
|
|
1580
|
-
diff.as(
|
|
1581
|
-
diff.as(
|
|
1582
|
-
diff.as(
|
|
1580
|
+
diff.as('day'); // Get as days
|
|
1581
|
+
diff.as('hour'); // Get as hours
|
|
1582
|
+
diff.as('minute'); // Get as minutes
|
|
1583
1583
|
diff.valueOf(); // Implicit number conversion (milliseconds)
|
|
1584
1584
|
|
|
1585
1585
|
// Locale-specific formatting
|
|
1586
1586
|
const spanishDiff = start.diff(end, {
|
|
1587
|
-
mode:
|
|
1588
|
-
locale:
|
|
1587
|
+
mode: 'calendar',
|
|
1588
|
+
locale: 'es',
|
|
1589
1589
|
});
|
|
1590
1590
|
console.log(spanishDiff.format()); // Uses Spanish locale
|
|
1591
1591
|
|