@trackunit/react-date-and-time-hooks 0.0.159 → 0.0.160
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/index.cjs.js +39 -2
- package/index.esm.js +40 -3
- package/package.json +1 -1
- package/src/useDateAndTime.d.ts +6 -3
package/index.cjs.js
CHANGED
|
@@ -173,8 +173,8 @@ const useDateAndTime = () => {
|
|
|
173
173
|
* @param {TemporalFormat} format
|
|
174
174
|
* @returns {string}
|
|
175
175
|
*/
|
|
176
|
-
const formatDate = react.useCallback((date, format) => {
|
|
177
|
-
return dateAndTimeUtils.formatDateUtil(date, format, currentTimeZone, currentLocale);
|
|
176
|
+
const formatDate = react.useCallback((date, format, hourCycle) => {
|
|
177
|
+
return dateAndTimeUtils.formatDateUtil(date, format, currentTimeZone, currentLocale, hourCycle);
|
|
178
178
|
}, [currentLocale, currentTimeZone]);
|
|
179
179
|
/**
|
|
180
180
|
* Formats a date using the custom locale and custom timezone.
|
|
@@ -334,6 +334,8 @@ const useDateAndTime = () => {
|
|
|
334
334
|
*/
|
|
335
335
|
const difference = react.useCallback((from, to, type) => {
|
|
336
336
|
switch (type) {
|
|
337
|
+
case "second":
|
|
338
|
+
return dateAndTimeUtils.differenceInSecondsUtil(from, to);
|
|
337
339
|
case "minute":
|
|
338
340
|
return dateAndTimeUtils.differenceInMinutesUtil(from, to);
|
|
339
341
|
case "hour":
|
|
@@ -344,6 +346,8 @@ const useDateAndTime = () => {
|
|
|
344
346
|
return dateAndTimeUtils.differenceInWeeksUtil(from, to);
|
|
345
347
|
case "month":
|
|
346
348
|
return dateAndTimeUtils.differenceInMonthsUtil(from, to);
|
|
349
|
+
case "year":
|
|
350
|
+
return dateAndTimeUtils.differenceInYearsUtil(from, to);
|
|
347
351
|
default:
|
|
348
352
|
return sharedUtils.exhaustiveCheck(type);
|
|
349
353
|
}
|
|
@@ -434,6 +438,28 @@ const useDateAndTime = () => {
|
|
|
434
438
|
: polyfill.Temporal.Duration.from(from).round({ smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "minute" });
|
|
435
439
|
return dateAndTimeUtils.getDurationFormat(durationValue, currentLocale, format);
|
|
436
440
|
}, [currentLocale]);
|
|
441
|
+
/**
|
|
442
|
+
* Returns the converted time between units.
|
|
443
|
+
*
|
|
444
|
+
* @param {number} amount - The number to convert.
|
|
445
|
+
* @param {Temporal.TimeUnit} unit - The unit to convert from.
|
|
446
|
+
* @param {"hour" | "minute" | "second" | "millisecond"} conversionType - The unit to convert to.
|
|
447
|
+
* @returns {number}
|
|
448
|
+
*/
|
|
449
|
+
const convert = react.useCallback((amount, unit, conversionType) => {
|
|
450
|
+
switch (unit) {
|
|
451
|
+
case "hour":
|
|
452
|
+
return dateAndTimeUtils.convertHoursUtil(amount, conversionType);
|
|
453
|
+
case "minute":
|
|
454
|
+
return dateAndTimeUtils.convertMinutesUtil(amount, conversionType);
|
|
455
|
+
case "second":
|
|
456
|
+
return dateAndTimeUtils.convertSecondsUtil(amount, conversionType);
|
|
457
|
+
case "millisecond":
|
|
458
|
+
return dateAndTimeUtils.convertMillisecondsUtil(amount, conversionType);
|
|
459
|
+
default:
|
|
460
|
+
return sharedUtils.exhaustiveCheck(unit);
|
|
461
|
+
}
|
|
462
|
+
}, []);
|
|
437
463
|
/**
|
|
438
464
|
* Returns the name of a day.
|
|
439
465
|
*
|
|
@@ -460,6 +486,13 @@ const useDateAndTime = () => {
|
|
|
460
486
|
const months = react.useCallback((format) => {
|
|
461
487
|
return dateAndTimeUtils.monthsUtil(format, currentLocale);
|
|
462
488
|
}, [currentLocale]);
|
|
489
|
+
/**
|
|
490
|
+
* Returns the UTC date from a time zoned date.
|
|
491
|
+
*
|
|
492
|
+
*/
|
|
493
|
+
const getUTCFromTimeZonedDate = react.useCallback((from, timeZoneId) => {
|
|
494
|
+
return dateAndTimeUtils.getUTCFromTimeZonedUtil(from, timeZoneId);
|
|
495
|
+
}, []);
|
|
463
496
|
return react.useMemo(() => ({
|
|
464
497
|
nowDate,
|
|
465
498
|
formatDate,
|
|
@@ -468,6 +501,7 @@ const useDateAndTime = () => {
|
|
|
468
501
|
startOf,
|
|
469
502
|
endOf,
|
|
470
503
|
add,
|
|
504
|
+
convert,
|
|
471
505
|
subtract,
|
|
472
506
|
difference,
|
|
473
507
|
since,
|
|
@@ -479,8 +513,10 @@ const useDateAndTime = () => {
|
|
|
479
513
|
months,
|
|
480
514
|
dayName,
|
|
481
515
|
days,
|
|
516
|
+
getUTCFromTimeZonedDate,
|
|
482
517
|
}), [
|
|
483
518
|
add,
|
|
519
|
+
convert,
|
|
484
520
|
dateIs,
|
|
485
521
|
dayName,
|
|
486
522
|
days,
|
|
@@ -490,6 +526,7 @@ const useDateAndTime = () => {
|
|
|
490
526
|
formatDate,
|
|
491
527
|
formatDateWithTimezone,
|
|
492
528
|
formatRange,
|
|
529
|
+
getUTCFromTimeZonedDate,
|
|
493
530
|
monthName,
|
|
494
531
|
months,
|
|
495
532
|
nowDate,
|
package/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Temporal } from '@js-temporal/polyfill';
|
|
2
|
-
import { getTimeZone, toZonedDateTimeUtil, toDateUtil, formatDateUtil, formatRangeUtil, subtractYearsUtil, subtractMonthsUtil, subtractWeeksUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, addYearsUtil, addMonthsUtil, addWeeksUtil, addDaysUtil, addHoursUtil, addMinutesUtil, startOfMonthUtil, startOfWeekUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, differenceInMonthsUtil, differenceInWeeksUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, isBetweenUtil, isTodayUtil, isFutureUtil, isPastUtil, isSameYearUtil, isSameMonthUtil, isSameWeekUtil, isSameDayUtil, timeSinceInYears, timeSinceInMonths, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInSeconds, timeSinceAuto, toDuration, getDurationFormat, dayNameUtil, daysUtil, monthNameUtil, monthsUtil, isEqualUtil } from '@trackunit/date-and-time-utils';
|
|
2
|
+
import { getTimeZone, toZonedDateTimeUtil, toDateUtil, formatDateUtil, formatRangeUtil, subtractYearsUtil, subtractMonthsUtil, subtractWeeksUtil, subtractDaysUtil, subtractHoursUtil, subtractMinutesUtil, addYearsUtil, addMonthsUtil, addWeeksUtil, addDaysUtil, addHoursUtil, addMinutesUtil, startOfMonthUtil, startOfWeekUtil, startOfDayUtil, startOfHourUtil, startOfMinuteUtil, endOfMonthUtil, endOfWeekUtil, endOfDayUtil, endOfHourUtil, endOfMinuteUtil, differenceInYearsUtil, differenceInMonthsUtil, differenceInWeeksUtil, differenceInDaysUtil, differenceInHoursUtil, differenceInMinutesUtil, differenceInSecondsUtil, isBetweenUtil, isTodayUtil, isFutureUtil, isPastUtil, isSameYearUtil, isSameMonthUtil, isSameWeekUtil, isSameDayUtil, timeSinceInYears, timeSinceInMonths, timeSinceInDays, timeSinceInHours, timeSinceInMinutes, timeSinceInSeconds, timeSinceAuto, toDuration, getDurationFormat, convertMillisecondsUtil, convertSecondsUtil, convertMinutesUtil, convertHoursUtil, dayNameUtil, daysUtil, monthNameUtil, monthsUtil, getUTCFromTimeZonedUtil, isEqualUtil } from '@trackunit/date-and-time-utils';
|
|
3
3
|
import { exhaustiveCheck } from '@trackunit/shared-utils';
|
|
4
4
|
import { useMemo, useCallback } from 'react';
|
|
5
5
|
import { useQuery } from '@apollo/client';
|
|
@@ -171,8 +171,8 @@ const useDateAndTime = () => {
|
|
|
171
171
|
* @param {TemporalFormat} format
|
|
172
172
|
* @returns {string}
|
|
173
173
|
*/
|
|
174
|
-
const formatDate = useCallback((date, format) => {
|
|
175
|
-
return formatDateUtil(date, format, currentTimeZone, currentLocale);
|
|
174
|
+
const formatDate = useCallback((date, format, hourCycle) => {
|
|
175
|
+
return formatDateUtil(date, format, currentTimeZone, currentLocale, hourCycle);
|
|
176
176
|
}, [currentLocale, currentTimeZone]);
|
|
177
177
|
/**
|
|
178
178
|
* Formats a date using the custom locale and custom timezone.
|
|
@@ -332,6 +332,8 @@ const useDateAndTime = () => {
|
|
|
332
332
|
*/
|
|
333
333
|
const difference = useCallback((from, to, type) => {
|
|
334
334
|
switch (type) {
|
|
335
|
+
case "second":
|
|
336
|
+
return differenceInSecondsUtil(from, to);
|
|
335
337
|
case "minute":
|
|
336
338
|
return differenceInMinutesUtil(from, to);
|
|
337
339
|
case "hour":
|
|
@@ -342,6 +344,8 @@ const useDateAndTime = () => {
|
|
|
342
344
|
return differenceInWeeksUtil(from, to);
|
|
343
345
|
case "month":
|
|
344
346
|
return differenceInMonthsUtil(from, to);
|
|
347
|
+
case "year":
|
|
348
|
+
return differenceInYearsUtil(from, to);
|
|
345
349
|
default:
|
|
346
350
|
return exhaustiveCheck(type);
|
|
347
351
|
}
|
|
@@ -432,6 +436,28 @@ const useDateAndTime = () => {
|
|
|
432
436
|
: Temporal.Duration.from(from).round({ smallestUnit: roundAt !== null && roundAt !== void 0 ? roundAt : "minute" });
|
|
433
437
|
return getDurationFormat(durationValue, currentLocale, format);
|
|
434
438
|
}, [currentLocale]);
|
|
439
|
+
/**
|
|
440
|
+
* Returns the converted time between units.
|
|
441
|
+
*
|
|
442
|
+
* @param {number} amount - The number to convert.
|
|
443
|
+
* @param {Temporal.TimeUnit} unit - The unit to convert from.
|
|
444
|
+
* @param {"hour" | "minute" | "second" | "millisecond"} conversionType - The unit to convert to.
|
|
445
|
+
* @returns {number}
|
|
446
|
+
*/
|
|
447
|
+
const convert = useCallback((amount, unit, conversionType) => {
|
|
448
|
+
switch (unit) {
|
|
449
|
+
case "hour":
|
|
450
|
+
return convertHoursUtil(amount, conversionType);
|
|
451
|
+
case "minute":
|
|
452
|
+
return convertMinutesUtil(amount, conversionType);
|
|
453
|
+
case "second":
|
|
454
|
+
return convertSecondsUtil(amount, conversionType);
|
|
455
|
+
case "millisecond":
|
|
456
|
+
return convertMillisecondsUtil(amount, conversionType);
|
|
457
|
+
default:
|
|
458
|
+
return exhaustiveCheck(unit);
|
|
459
|
+
}
|
|
460
|
+
}, []);
|
|
435
461
|
/**
|
|
436
462
|
* Returns the name of a day.
|
|
437
463
|
*
|
|
@@ -458,6 +484,13 @@ const useDateAndTime = () => {
|
|
|
458
484
|
const months = useCallback((format) => {
|
|
459
485
|
return monthsUtil(format, currentLocale);
|
|
460
486
|
}, [currentLocale]);
|
|
487
|
+
/**
|
|
488
|
+
* Returns the UTC date from a time zoned date.
|
|
489
|
+
*
|
|
490
|
+
*/
|
|
491
|
+
const getUTCFromTimeZonedDate = useCallback((from, timeZoneId) => {
|
|
492
|
+
return getUTCFromTimeZonedUtil(from, timeZoneId);
|
|
493
|
+
}, []);
|
|
461
494
|
return useMemo(() => ({
|
|
462
495
|
nowDate,
|
|
463
496
|
formatDate,
|
|
@@ -466,6 +499,7 @@ const useDateAndTime = () => {
|
|
|
466
499
|
startOf,
|
|
467
500
|
endOf,
|
|
468
501
|
add,
|
|
502
|
+
convert,
|
|
469
503
|
subtract,
|
|
470
504
|
difference,
|
|
471
505
|
since,
|
|
@@ -477,8 +511,10 @@ const useDateAndTime = () => {
|
|
|
477
511
|
months,
|
|
478
512
|
dayName,
|
|
479
513
|
days,
|
|
514
|
+
getUTCFromTimeZonedDate,
|
|
480
515
|
}), [
|
|
481
516
|
add,
|
|
517
|
+
convert,
|
|
482
518
|
dateIs,
|
|
483
519
|
dayName,
|
|
484
520
|
days,
|
|
@@ -488,6 +524,7 @@ const useDateAndTime = () => {
|
|
|
488
524
|
formatDate,
|
|
489
525
|
formatDateWithTimezone,
|
|
490
526
|
formatRange,
|
|
527
|
+
getUTCFromTimeZonedDate,
|
|
491
528
|
monthName,
|
|
492
529
|
months,
|
|
493
530
|
nowDate,
|
package/package.json
CHANGED
package/src/useDateAndTime.d.ts
CHANGED
|
@@ -2,24 +2,26 @@ import { DateRange, TemporalDate, TemporalFormat, TemporalFormatStyle } from "@t
|
|
|
2
2
|
type TemporalTypes = "second" | "seconds" | "minute" | "minutes" | "hour" | "hours" | "day" | "days" | "week" | "weeks" | "month" | "months" | "year" | "years";
|
|
3
3
|
export type TemporalArithmeticType = Extract<TemporalTypes, "hours" | "minutes" | "days" | "weeks" | "months" | "years">;
|
|
4
4
|
export type TemporalEdgeOfType = Extract<TemporalTypes, "hour" | "minute" | "day" | "week" | "month">;
|
|
5
|
-
export type TemporalDifferenceType = Extract<TemporalTypes, "minute" | "hour" | "day" | "week" | "month">;
|
|
5
|
+
export type TemporalDifferenceType = Extract<TemporalTypes, "second" | "minute" | "hour" | "day" | "week" | "month" | "year">;
|
|
6
6
|
export type TemporalSinceType = "auto" | Extract<TemporalTypes, "seconds" | "minutes" | "hours" | "days" | "months" | "years">;
|
|
7
|
-
export type TemporalDurationType = Extract<TemporalTypes, "days" | "hours" | "minutes">;
|
|
7
|
+
export type TemporalDurationType = Extract<TemporalTypes, "days" | "hours" | "minutes" | "seconds">;
|
|
8
8
|
export type TemporalIsType = "past" | "present" | "future" | "between";
|
|
9
9
|
export type TemporalSameType = Extract<TemporalTypes, "day" | "week" | "month" | "year">;
|
|
10
10
|
export type TemporalTimeRounding = Extract<TemporalTypes, "hour" | "minute" | "second">;
|
|
11
|
+
export type HourCycle = "h12" | "h23";
|
|
11
12
|
/**
|
|
12
13
|
* Hook for managing date and time operations.
|
|
13
14
|
*
|
|
14
15
|
*/
|
|
15
16
|
export declare const useDateAndTime: () => {
|
|
16
17
|
nowDate: Date;
|
|
17
|
-
formatDate: (date: TemporalDate, format?: TemporalFormat) => string;
|
|
18
|
+
formatDate: (date: TemporalDate, format?: TemporalFormat, hourCycle?: HourCycle) => string;
|
|
18
19
|
formatDateWithTimezone: (date: TemporalDate, format?: TemporalFormat, timezone?: string, locale?: string) => string;
|
|
19
20
|
formatRange: (range: DateRange, format?: Intl.DateTimeFormatOptions) => string;
|
|
20
21
|
startOf: (date: TemporalDate, type: TemporalEdgeOfType) => Date;
|
|
21
22
|
endOf: (date: TemporalDate, type: TemporalEdgeOfType) => Date;
|
|
22
23
|
add: (date: TemporalDate, amount: number, type: TemporalArithmeticType) => Date;
|
|
24
|
+
convert: (amount: number, unit: "hour" | "minute" | "second" | "millisecond", conversionType: "hour" | "minute" | "second" | "millisecond") => number;
|
|
23
25
|
subtract: (date: TemporalDate, amount: number, type: TemporalArithmeticType) => Date;
|
|
24
26
|
difference: (from: TemporalDate, to: TemporalDate, type: TemporalDifferenceType) => number;
|
|
25
27
|
since: (from: TemporalDate, to: TemporalDate, type: TemporalSinceType) => string;
|
|
@@ -31,5 +33,6 @@ export declare const useDateAndTime: () => {
|
|
|
31
33
|
months: (format?: TemporalFormatStyle) => string[];
|
|
32
34
|
dayName: (date: TemporalDate, format: TemporalFormatStyle) => string;
|
|
33
35
|
days: (format?: TemporalFormatStyle) => string[];
|
|
36
|
+
getUTCFromTimeZonedDate: (from: Date, timeZoneId: string) => Date;
|
|
34
37
|
};
|
|
35
38
|
export {};
|