@formkit/tempo 1.0.0 → 1.1.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.
Files changed (48) hide show
  1. package/README.md +22 -1
  2. package/dist/add.d.ts +11 -0
  3. package/dist/add.mjs +50 -0
  4. package/dist/add.mjs.map +1 -0
  5. package/dist/addMonth.mjs +2 -11
  6. package/dist/addMonth.mjs.map +1 -1
  7. package/dist/addYear.mjs +6 -11
  8. package/dist/addYear.mjs.map +1 -1
  9. package/dist/bundle.d.ts +110 -1
  10. package/dist/bundle.mjs +214 -46
  11. package/dist/bundle.mjs.map +1 -1
  12. package/dist/diff.d.ts +38 -0
  13. package/dist/diff.mjs +86 -0
  14. package/dist/diff.mjs.map +1 -0
  15. package/dist/handleDateOverflow.d.ts +12 -0
  16. package/dist/handleDateOverflow.mjs +18 -0
  17. package/dist/handleDateOverflow.mjs.map +1 -0
  18. package/dist/index.cjs +223 -46
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +110 -1
  21. package/dist/index.d.ts +10 -1
  22. package/dist/index.mjs +18 -0
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/offset.mjs +14 -12
  25. package/dist/offset.mjs.map +1 -1
  26. package/dist/setDayOfMonth.d.ts +11 -0
  27. package/dist/setDayOfMonth.mjs +16 -0
  28. package/dist/setDayOfMonth.mjs.map +1 -0
  29. package/dist/setHour.d.ts +10 -0
  30. package/dist/setHour.mjs +11 -0
  31. package/dist/setHour.mjs.map +1 -0
  32. package/dist/setMilliseconds.d.ts +10 -0
  33. package/dist/setMilliseconds.mjs +11 -0
  34. package/dist/setMilliseconds.mjs.map +1 -0
  35. package/dist/setMinutes.d.ts +10 -0
  36. package/dist/setMinutes.mjs +11 -0
  37. package/dist/setMinutes.mjs.map +1 -0
  38. package/dist/setMonth.d.ts +11 -0
  39. package/dist/setMonth.mjs +9 -0
  40. package/dist/setMonth.mjs.map +1 -0
  41. package/dist/setSeconds.d.ts +10 -0
  42. package/dist/setSeconds.mjs +11 -0
  43. package/dist/setSeconds.mjs.map +1 -0
  44. package/dist/setYear.d.ts +11 -0
  45. package/dist/setYear.mjs +9 -0
  46. package/dist/setYear.mjs.map +1 -0
  47. package/dist/types.d.ts +15 -1
  48. package/package.json +3 -3
package/README.md CHANGED
@@ -10,7 +10,28 @@ Tempo is a new library in a proud tradition of JavaScript date and time librarie
10
10
 
11
11
  Tempo is best thought of as a collection of utilities for working with `Date` objects — an important distinction from other libraries that provide custom date primitives. Under the hood, Tempo mines JavaScript's `Intl.DateTimeFormat` to extract complex data like timezones offsets and locale aware date formats giving you a simple API to format, parse, and manipulates dates.
12
12
 
13
- Tempo is tiny tree-shakable framework, you can only take what you need. All functionality is available in **5.2 kB for esm** and **5.5 kB for cjs** modules (minified and brotlied). [Size Limit](https://github.com/ai/size-limit) controls the size.
13
+ Tempo is tiny tree-shakable framework, you can only take what you need. You can work with timezones with only a few bytes of library code, or perform full international date formatting for ~2Kb (minified and brotlied). [Size Limit](https://github.com/ai/size-limit) controls the size.
14
+
15
+ ## Development
16
+
17
+ Install from the repository root with `pnpm`:
18
+
19
+ ```bash
20
+ corepack enable
21
+ pnpm install
22
+ ```
23
+
24
+ Build the library:
25
+
26
+ ```bash
27
+ pnpm build
28
+ ```
29
+
30
+ Build the docs app:
31
+
32
+ ```bash
33
+ pnpm docs-build
34
+ ```
14
35
 
15
36
  <a href="https://tempo.formkit.com">
16
37
  <img src="docs/public/read-the-docs.png" alt="Read the docs" width="200" height="43">
package/dist/add.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { MaybeDateInput, Duration } from './types.js';
2
+
3
+ /**
4
+ * Returns a new Date object with the duration applied.
5
+ * @param [inputDate] - A date to increment or null to increment from the current time.
6
+ * @param duration - An object with values for the amount of time to add to the original date.
7
+ * @param [dateOverflow] - Whether to allow month/year changes to overflow into a later month.
8
+ */
9
+ declare function add(inputDate: MaybeDateInput, duration: Duration, dateOverflow?: boolean): Date;
10
+
11
+ export { add };
package/dist/add.mjs ADDED
@@ -0,0 +1,50 @@
1
+ // src/add.ts
2
+ import { addDay } from "./addDay.mjs";
3
+ import { addHour } from "./addHour.mjs";
4
+ import { addMillisecond } from "./addMillisecond.mjs";
5
+ import { addMinute } from "./addMinute.mjs";
6
+ import { addMonth } from "./addMonth.mjs";
7
+ import { addSecond } from "./addSecond.mjs";
8
+ import { addYear } from "./addYear.mjs";
9
+ import { date } from "./date.mjs";
10
+ function add(inputDate, duration, dateOverflow = false) {
11
+ var _a, _b;
12
+ let d = date(inputDate);
13
+ const applyFixedUnits = () => {
14
+ if (duration.weeks) {
15
+ d = addDay(d, duration.weeks * 7);
16
+ }
17
+ if (duration.days) {
18
+ d = addDay(d, duration.days);
19
+ }
20
+ if (duration.hours) {
21
+ d = addHour(d, duration.hours);
22
+ }
23
+ if (duration.minutes) {
24
+ d = addMinute(d, duration.minutes);
25
+ }
26
+ if (duration.seconds) {
27
+ d = addSecond(d, duration.seconds);
28
+ }
29
+ if (duration.milliseconds) {
30
+ d = addMillisecond(d, duration.milliseconds);
31
+ }
32
+ };
33
+ const applyCalendarUnits = () => {
34
+ if (duration.months) {
35
+ d = addMonth(d, duration.months, dateOverflow);
36
+ }
37
+ if (duration.years) {
38
+ d = addYear(d, duration.years, dateOverflow);
39
+ }
40
+ };
41
+ const calendarFirst = ((_a = duration.months) != null ? _a : 0) < 0 || ((_b = duration.years) != null ? _b : 0) < 0;
42
+ if (calendarFirst) applyCalendarUnits();
43
+ applyFixedUnits();
44
+ if (!calendarFirst) applyCalendarUnits();
45
+ return d;
46
+ }
47
+ export {
48
+ add
49
+ };
50
+ //# sourceMappingURL=add.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/add.ts"],"sourcesContent":["import { addDay } from \"./addDay\"\nimport { addHour } from \"./addHour\"\nimport { addMillisecond } from \"./addMillisecond\"\nimport { addMinute } from \"./addMinute\"\nimport { addMonth } from \"./addMonth\"\nimport { addSecond } from \"./addSecond\"\nimport { addYear } from \"./addYear\"\nimport { date } from \"./date\"\nimport type { Duration, MaybeDateInput } from \"./types\"\n\n/**\n * Returns a new Date object with the duration applied.\n * @param [inputDate] - A date to increment or null to increment from the current time.\n * @param duration - An object with values for the amount of time to add to the original date.\n * @param [dateOverflow] - Whether to allow month/year changes to overflow into a later month.\n */\nexport function add(\n inputDate: MaybeDateInput,\n duration: Duration,\n dateOverflow = false\n) {\n let d = date(inputDate)\n const applyFixedUnits = () => {\n if (duration.weeks) {\n d = addDay(d, duration.weeks * 7)\n }\n if (duration.days) {\n d = addDay(d, duration.days)\n }\n if (duration.hours) {\n d = addHour(d, duration.hours)\n }\n if (duration.minutes) {\n d = addMinute(d, duration.minutes)\n }\n if (duration.seconds) {\n d = addSecond(d, duration.seconds)\n }\n if (duration.milliseconds) {\n d = addMillisecond(d, duration.milliseconds)\n }\n }\n const applyCalendarUnits = () => {\n if (duration.months) {\n d = addMonth(d, duration.months, dateOverflow)\n }\n if (duration.years) {\n d = addYear(d, duration.years, dateOverflow)\n }\n }\n const calendarFirst = (duration.months ?? 0) < 0 || (duration.years ?? 0) < 0\n\n if (calendarFirst) applyCalendarUnits()\n applyFixedUnits()\n if (!calendarFirst) applyCalendarUnits()\n\n return d\n}\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,eAAe;AACxB,SAAS,sBAAsB;AAC/B,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AACxB,SAAS,YAAY;AASd,SAAS,IACd,WACA,UACA,eAAe,OACf;AApBF;AAqBE,MAAI,IAAI,KAAK,SAAS;AACtB,QAAM,kBAAkB,MAAM;AAC5B,QAAI,SAAS,OAAO;AAClB,UAAI,OAAO,GAAG,SAAS,QAAQ,CAAC;AAAA,IAClC;AACA,QAAI,SAAS,MAAM;AACjB,UAAI,OAAO,GAAG,SAAS,IAAI;AAAA,IAC7B;AACA,QAAI,SAAS,OAAO;AAClB,UAAI,QAAQ,GAAG,SAAS,KAAK;AAAA,IAC/B;AACA,QAAI,SAAS,SAAS;AACpB,UAAI,UAAU,GAAG,SAAS,OAAO;AAAA,IACnC;AACA,QAAI,SAAS,SAAS;AACpB,UAAI,UAAU,GAAG,SAAS,OAAO;AAAA,IACnC;AACA,QAAI,SAAS,cAAc;AACzB,UAAI,eAAe,GAAG,SAAS,YAAY;AAAA,IAC7C;AAAA,EACF;AACA,QAAM,qBAAqB,MAAM;AAC/B,QAAI,SAAS,QAAQ;AACnB,UAAI,SAAS,GAAG,SAAS,QAAQ,YAAY;AAAA,IAC/C;AACA,QAAI,SAAS,OAAO;AAClB,UAAI,QAAQ,GAAG,SAAS,OAAO,YAAY;AAAA,IAC7C;AAAA,EACF;AACA,QAAM,kBAAiB,cAAS,WAAT,YAAmB,KAAK,OAAM,cAAS,UAAT,YAAkB,KAAK;AAE5E,MAAI,cAAe,oBAAmB;AACtC,kBAAgB;AAChB,MAAI,CAAC,cAAe,oBAAmB;AAEvC,SAAO;AACT;","names":[]}
package/dist/addMonth.mjs CHANGED
@@ -1,16 +1,7 @@
1
1
  // src/addMonth.ts
2
- import { date } from "./date.mjs";
3
- import { monthDays } from "./monthDays.mjs";
2
+ import { handleOverflow } from "./handleDateOverflow.mjs";
4
3
  function addMonth(inputDate, count = 1, dateOverflow = false) {
5
- const d = date(inputDate);
6
- const dayOfMonth = d.getDate();
7
- if (!dateOverflow) d.setDate(1);
8
- d.setMonth(d.getMonth() + count);
9
- if (!dateOverflow) {
10
- const daysInMonth = monthDays(d);
11
- d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
12
- }
13
- return d;
4
+ return handleOverflow(inputDate, (d) => d.setMonth(d.getMonth() + count), dateOverflow);
14
5
  }
15
6
  export {
16
7
  addMonth
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/addMonth.ts"],"sourcesContent":["import { date } from \"./date\"\nimport { monthDays } from \"./monthDays\"\nimport type { DateInput, MaybeDateInput } from \"./types\"\n\n/**\n * Returns a new date object 1/n months after the original one. Keep in mind if you\n * start with a date late in a given month you could get a date after the next\n * month.\n * @param [inputDate] - A date to increment or null to increment from the current time\n * @param [count] - The quantity to add.\n * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.\n */\nexport function addMonth(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) {\n const d = date(inputDate)\n const dayOfMonth = d.getDate()\n // If overflowing is disallowed, set the date back to the first of the month\n if (!dateOverflow) d.setDate(1)\n d.setMonth(d.getMonth() + count)\n\n // If overflowing is disallowed, we need to set the date back to the proper\n // day or the last day of the month.\n if (!dateOverflow) {\n const daysInMonth = monthDays(d)\n d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth)\n }\n return d\n}\n"],"mappings":";AAAA,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAWnB,SAAS,SAAS,WAA4B,QAAQ,GAAG,eAAe,OAAO;AACpF,QAAM,IAAI,KAAK,SAAS;AACxB,QAAM,aAAa,EAAE,QAAQ;AAE7B,MAAI,CAAC,aAAc,GAAE,QAAQ,CAAC;AAC9B,IAAE,SAAS,EAAE,SAAS,IAAI,KAAK;AAI/B,MAAI,CAAC,cAAc;AACjB,UAAM,cAAc,UAAU,CAAC;AAC/B,MAAE,QAAQ,cAAc,aAAa,cAAc,UAAU;AAAA,EAC/D;AACA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/addMonth.ts"],"sourcesContent":["import { handleOverflow } from \"./handleDateOverflow\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Returns a new date object 1/n months after the original one. Keep in mind if you\n * start with a date late in a given month you could get a date after the next\n * month.\n * @param [inputDate] - A date to increment or null to increment from the current time\n * @param [count] - The quantity to add.\n * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.\n */\nexport function addMonth(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) {\n return handleOverflow(inputDate, (d) => d.setMonth(d.getMonth() + count), dateOverflow)\n}\n"],"mappings":";AAAA,SAAS,sBAAsB;AAWxB,SAAS,SAAS,WAA4B,QAAQ,GAAG,eAAe,OAAO;AACpF,SAAO,eAAe,WAAW,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,IAAI,KAAK,GAAG,YAAY;AACxF;","names":[]}
package/dist/addYear.mjs CHANGED
@@ -1,16 +1,11 @@
1
1
  // src/addYear.ts
2
- import { date } from "./date.mjs";
3
- import { monthDays } from "./monthDays.mjs";
2
+ import { handleOverflow } from "./handleDateOverflow.mjs";
4
3
  function addYear(inputDate, count = 1, dateOverflow = false) {
5
- const d = date(inputDate);
6
- const dayOfMonth = d.getDate();
7
- if (!dateOverflow) d.setDate(1);
8
- d.setFullYear(d.getFullYear() + count);
9
- if (!dateOverflow) {
10
- const daysInMonth = monthDays(d);
11
- d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
12
- }
13
- return d;
4
+ return handleOverflow(
5
+ inputDate,
6
+ (d) => d.setFullYear(d.getFullYear() + count),
7
+ dateOverflow
8
+ );
14
9
  }
15
10
  export {
16
11
  addYear
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/addYear.ts"],"sourcesContent":["import { date } from \"./date\"\nimport { monthDays } from \"./monthDays\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Returns a new date object 1/n years after the original one. Keep in mind if\n * you start with a date late in a given month you could get a date after the\n * next month.\n * @param [inputDate] - A date to increment or null to increment from the current time.\n * @param [count] - The quantity of years add.\n * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.\n */\nexport function addYear(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) {\n const d = date(inputDate)\n const dayOfMonth = d.getDate()\n // If overflowing is disallowed, set the date back to the first of the month\n if (!dateOverflow) d.setDate(1)\n\n d.setFullYear(d.getFullYear() + count)\n\n // If overflowing is disallowed, we need to set the date back to the proper\n // day or the last day of the month.\n if (!dateOverflow) {\n const daysInMonth = monthDays(d)\n d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth)\n }\n return d\n}\n"],"mappings":";AAAA,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAWnB,SAAS,QAAQ,WAA4B,QAAQ,GAAG,eAAe,OAAO;AACnF,QAAM,IAAI,KAAK,SAAS;AACxB,QAAM,aAAa,EAAE,QAAQ;AAE7B,MAAI,CAAC,aAAc,GAAE,QAAQ,CAAC;AAE9B,IAAE,YAAY,EAAE,YAAY,IAAI,KAAK;AAIrC,MAAI,CAAC,cAAc;AACjB,UAAM,cAAc,UAAU,CAAC;AAC/B,MAAE,QAAQ,cAAc,aAAa,cAAc,UAAU;AAAA,EAC/D;AACA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/addYear.ts"],"sourcesContent":["import { handleOverflow } from \"./handleDateOverflow\"\nimport type { MaybeDateInput } from \"./types\"\n\n/**\n * Returns a new date object 1/n years after the original one. Keep in mind if\n * you start with a date late in a given month you could get a date after the\n * next month.\n * @param [inputDate] - A date to increment or null to increment from the current time.\n * @param [count] - The quantity of years add.\n * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.\n */\nexport function addYear(inputDate?: MaybeDateInput, count = 1, dateOverflow = false) {\n return handleOverflow(\n inputDate,\n (d) => d.setFullYear(d.getFullYear() + count),\n dateOverflow\n )\n}\n"],"mappings":";AAAA,SAAS,sBAAsB;AAWxB,SAAS,QAAQ,WAA4B,QAAQ,GAAG,eAAe,OAAO;AACnF,SAAO;AAAA,IACL;AAAA,IACA,CAAC,MAAM,EAAE,YAAY,EAAE,YAAY,IAAI,KAAK;AAAA,IAC5C;AAAA,EACF;AACF;","names":[]}
package/dist/bundle.d.ts CHANGED
@@ -147,6 +147,28 @@ interface FormatOptions {
147
147
  */
148
148
  partFilter?: (part: Part) => boolean;
149
149
  }
150
+ interface Duration {
151
+ years?: number;
152
+ months?: number;
153
+ weeks?: number;
154
+ days?: number;
155
+ hours?: number;
156
+ minutes?: number;
157
+ seconds?: number;
158
+ milliseconds?: number;
159
+ }
160
+ interface DurationObj extends Duration {
161
+ microseconds?: number;
162
+ nanoseconds?: number;
163
+ }
164
+
165
+ /**
166
+ * Returns a new Date object with the duration applied.
167
+ * @param [inputDate] - A date to increment or null to increment from the current time.
168
+ * @param duration - An object with values for the amount of time to add to the original date.
169
+ * @param [dateOverflow] - Whether to allow month/year changes to overflow into a later month.
170
+ */
171
+ declare function add(inputDate: MaybeDateInput, duration: Duration, dateOverflow?: boolean): Date;
150
172
 
151
173
  /**
152
174
  * Returns a new date object 1/n days after the original one.
@@ -497,6 +519,58 @@ declare function sameYear(inputDateA: DateInput, inputDateB?: MaybeDateInput): b
497
519
  */
498
520
  declare function sameYear(inputDateA: MaybeDateInput, inputDateB: DateInput): boolean;
499
521
 
522
+ /**
523
+ * set the millisecond of a date object
524
+ * @param [inputDate] - a date or null for current time
525
+ * @param ms - the milliseconds you want the date set to (0 - 999) (can over/underflow)
526
+ */
527
+ declare function setMilliseconds(inputDate: MaybeDateInput, ms: number): Date;
528
+
529
+ /**
530
+ * set the second of a date object
531
+ * @param [inputDate] - a date or null for current time
532
+ * @param second - the second you want the date set to (0 - 59) (can over/underflow)
533
+ */
534
+ declare function setSeconds(inputDate: MaybeDateInput, second: number): Date;
535
+
536
+ /**
537
+ * set the minute of a date object
538
+ * @param [inputDate] - a date or null for current time
539
+ * @param minute - the minute you want the date set to (0 - 59) (can over/underflow)
540
+ */
541
+ declare function setMinutes(inputDate: MaybeDateInput, minute: number): Date;
542
+
543
+ /**
544
+ * set the hour of a date object
545
+ * @param [inputDate] - a date or null for current time
546
+ * @param hour - the hour you want the date set to (0 - 23) (can over/underflow)
547
+ */
548
+ declare function setHour(inputDate: MaybeDateInput, hour: number): Date;
549
+
550
+ /**
551
+ * set the day of the month in a date object
552
+ * @param [inputDate] - a date or null for current time
553
+ * @param day - the day of the month you want the date set to (1-28/29/30/31) (can over/underflow)
554
+ * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the given day isn't in the current month
555
+ */
556
+ declare function setDayOfMonth(inputDate: MaybeDateInput, day: number, dateOverflow?: boolean): Date;
557
+
558
+ /**
559
+ * set the month of a date
560
+ * @param [inputDate] - a date or null for current time
561
+ * @param month - the zero-based month to set (0-11, where 0 is January) (can over/underflow)
562
+ * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.
563
+ */
564
+ declare function setMonth(inputDate: MaybeDateInput, month: number, dateOverflow?: boolean): Date;
565
+
566
+ /**
567
+ * set the year of a date object
568
+ * @param inputDate - a date or null for current time
569
+ * @param year - the full year you want the date to be set to
570
+ * @param [dateOverflow] - Whether or not to allow the date to overflow to another month if the inputDate’s month is out of range of the new month.
571
+ */
572
+ declare function setYear(inputDate: MaybeDateInput, year: number, dateOverflow?: boolean): Date;
573
+
500
574
  /**
501
575
  * Returns a Date object for the last day at the last second of the given week.
502
576
  * Defaults to Sunday as the first day of the week:
@@ -707,4 +781,39 @@ declare function diffYears(dateA: DateInput, dateB?: MaybeDateInput): number;
707
781
  */
708
782
  declare function diffYears(dateA: MaybeDateInput, dateB: DateInput): number;
709
783
 
710
- export { type DateInput, type ExtendedPartTypes, type FilledPart, type Format, type FormatOptions, type FormatPattern, type FormatStyle, type FormatStyleObj, type FormatToken, type MaybeDateInput, type NamedFormatOption, type NamedFormats, type ParseOptions, type Part, addDay, addHour, addMillisecond, addMinute, addMonth, addSecond, addYear, ap, applyOffset, date, dayEnd, dayOfYear, dayStart, diffDays, diffHours, diffMilliseconds, diffMinutes, diffMonths, diffSeconds, diffWeeks, diffYears, format, formatStr, fourDigitYear, hourEnd, hourStart, isAfter, isBefore, isEqual, isFuture, isPast, iso8601, minuteEnd, minuteStart, monthDays, monthEnd, monthStart, nearestDay, offset, parse, parseParts, parts, range, removeOffset, sameDay, sameHour, sameMillisecond, sameMinute, sameSecond, sameYear, tzDate, weekEnd, weekStart, yearDays, yearEnd, yearStart };
784
+ type DurationKey = keyof Duration;
785
+ /**
786
+ * Options for `diff` function
787
+ */
788
+ interface DiffOptions {
789
+ /**
790
+ * whether the difference should be absolute (not negative)
791
+ */
792
+ abs?: boolean;
793
+ /**
794
+ * units you want to skip, for example weeks
795
+ */
796
+ skip?: DurationKey[] | Set<DurationKey>;
797
+ }
798
+ /**
799
+ * Returns the difference between 2 dates in an object
800
+ * @param dateA - A date to compare with the right date
801
+ * @param [dateB] - A date to compare with the left date or nothing to compare with the current time
802
+ * @param [options] additional options
803
+ * @param [options.skip] units you want skip
804
+ * @param [options.abs] whether the difference should be absolute
805
+ * @returns an object which could be used with `Intl.DurationFormat.format'`
806
+ */
807
+ declare function diff(dateA: DateInput, dateB?: MaybeDateInput, options?: DiffOptions): Duration;
808
+ /**
809
+ * Returns the difference between 2 dates in an object
810
+ * @param [dateA] - A date to compare with the right date or null to compare with the current time
811
+ * @param dateB - A date to compare with the left date
812
+ * @param [options] additional options
813
+ * @param [options.skip] units you want skip
814
+ * @param [options.abs] whether the difference should be absolute
815
+ * @returns an object which could be used with `Intl.DurationFormat.format'`
816
+ */
817
+ declare function diff(dateA: MaybeDateInput, dateB: DateInput, options?: DiffOptions): Duration;
818
+
819
+ export { type DateInput, type DiffOptions, type Duration, type DurationObj, type ExtendedPartTypes, type FilledPart, type Format, type FormatOptions, type FormatPattern, type FormatStyle, type FormatStyleObj, type FormatToken, type MaybeDateInput, type NamedFormatOption, type NamedFormats, type ParseOptions, type Part, add, addDay, addHour, addMillisecond, addMinute, addMonth, addSecond, addYear, ap, applyOffset, date, dayEnd, dayOfYear, dayStart, diff, diffDays, diffHours, diffMilliseconds, diffMinutes, diffMonths, diffSeconds, diffWeeks, diffYears, format, formatStr, fourDigitYear, hourEnd, hourStart, isAfter, isBefore, isEqual, isFuture, isPast, iso8601, minuteEnd, minuteStart, monthDays, monthEnd, monthStart, nearestDay, offset, parse, parseParts, parts, range, removeOffset, sameDay, sameHour, sameMillisecond, sameMinute, sameSecond, sameYear, setDayOfMonth, setHour, setMilliseconds, setMinutes, setMonth, setSeconds, setYear, tzDate, weekEnd, weekStart, yearDays, yearEnd, yearStart };
package/dist/bundle.mjs CHANGED
@@ -47,6 +47,27 @@ function addDay(inputDate, count = 1) {
47
47
  return d;
48
48
  }
49
49
 
50
+ // src/addHour.ts
51
+ function addHour(inputDate, count = 1) {
52
+ const d = date(inputDate);
53
+ d.setHours(d.getHours() + count);
54
+ return d;
55
+ }
56
+
57
+ // src/addMillisecond.ts
58
+ function addMillisecond(inputDate, count = 1) {
59
+ const d = date(inputDate);
60
+ d.setMilliseconds(d.getMilliseconds() + count);
61
+ return d;
62
+ }
63
+
64
+ // src/addMinute.ts
65
+ function addMinute(inputDate, count = 1) {
66
+ const d = date(inputDate);
67
+ d.setMinutes(d.getMinutes() + count);
68
+ return d;
69
+ }
70
+
50
71
  // src/monthEnd.ts
51
72
  function monthEnd(inputDate) {
52
73
  const d = date(inputDate);
@@ -62,25 +83,12 @@ function monthDays(inputDate) {
62
83
  return d.getDate();
63
84
  }
64
85
 
65
- // src/addMonth.ts
66
- function addMonth(inputDate, count = 1, dateOverflow = false) {
67
- const d = date(inputDate);
68
- const dayOfMonth = d.getDate();
69
- if (!dateOverflow) d.setDate(1);
70
- d.setMonth(d.getMonth() + count);
71
- if (!dateOverflow) {
72
- const daysInMonth = monthDays(d);
73
- d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
74
- }
75
- return d;
76
- }
77
-
78
- // src/addYear.ts
79
- function addYear(inputDate, count = 1, dateOverflow = false) {
86
+ // src/handleDateOverflow.ts
87
+ function handleOverflow(inputDate, action, dateOverflow = false) {
80
88
  const d = date(inputDate);
81
89
  const dayOfMonth = d.getDate();
82
90
  if (!dateOverflow) d.setDate(1);
83
- d.setFullYear(d.getFullYear() + count);
91
+ action(d);
84
92
  if (!dateOverflow) {
85
93
  const daysInMonth = monthDays(d);
86
94
  d.setDate(daysInMonth < dayOfMonth ? daysInMonth : dayOfMonth);
@@ -88,18 +96,9 @@ function addYear(inputDate, count = 1, dateOverflow = false) {
88
96
  return d;
89
97
  }
90
98
 
91
- // src/addHour.ts
92
- function addHour(inputDate, count = 1) {
93
- const d = date(inputDate);
94
- d.setHours(d.getHours() + count);
95
- return d;
96
- }
97
-
98
- // src/addMinute.ts
99
- function addMinute(inputDate, count = 1) {
100
- const d = date(inputDate);
101
- d.setMinutes(d.getMinutes() + count);
102
- return d;
99
+ // src/addMonth.ts
100
+ function addMonth(inputDate, count = 1, dateOverflow = false) {
101
+ return handleOverflow(inputDate, (d) => d.setMonth(d.getMonth() + count), dateOverflow);
103
102
  }
104
103
 
105
104
  // src/addSecond.ts
@@ -109,10 +108,51 @@ function addSecond(inputDate, count = 1) {
109
108
  return d;
110
109
  }
111
110
 
112
- // src/addMillisecond.ts
113
- function addMillisecond(inputDate, count = 1) {
114
- const d = date(inputDate);
115
- d.setMilliseconds(d.getMilliseconds() + count);
111
+ // src/addYear.ts
112
+ function addYear(inputDate, count = 1, dateOverflow = false) {
113
+ return handleOverflow(
114
+ inputDate,
115
+ (d) => d.setFullYear(d.getFullYear() + count),
116
+ dateOverflow
117
+ );
118
+ }
119
+
120
+ // src/add.ts
121
+ function add(inputDate, duration, dateOverflow = false) {
122
+ var _a, _b;
123
+ let d = date(inputDate);
124
+ const applyFixedUnits = () => {
125
+ if (duration.weeks) {
126
+ d = addDay(d, duration.weeks * 7);
127
+ }
128
+ if (duration.days) {
129
+ d = addDay(d, duration.days);
130
+ }
131
+ if (duration.hours) {
132
+ d = addHour(d, duration.hours);
133
+ }
134
+ if (duration.minutes) {
135
+ d = addMinute(d, duration.minutes);
136
+ }
137
+ if (duration.seconds) {
138
+ d = addSecond(d, duration.seconds);
139
+ }
140
+ if (duration.milliseconds) {
141
+ d = addMillisecond(d, duration.milliseconds);
142
+ }
143
+ };
144
+ const applyCalendarUnits = () => {
145
+ if (duration.months) {
146
+ d = addMonth(d, duration.months, dateOverflow);
147
+ }
148
+ if (duration.years) {
149
+ d = addYear(d, duration.years, dateOverflow);
150
+ }
151
+ };
152
+ const calendarFirst = ((_a = duration.months) != null ? _a : 0) < 0 || ((_b = duration.years) != null ? _b : 0) < 0;
153
+ if (calendarFirst) applyCalendarUnits();
154
+ applyFixedUnits();
155
+ if (!calendarFirst) applyCalendarUnits();
116
156
  return d;
117
157
  }
118
158
 
@@ -381,22 +421,24 @@ function deviceTZ() {
381
421
  // src/offset.ts
382
422
  function relativeTime(d, timeZone) {
383
423
  const utcParts = new Intl.DateTimeFormat("en-US", {
424
+ era: "short",
384
425
  year: "numeric",
385
- month: "2-digit",
386
- day: "2-digit",
387
- hour: "2-digit",
388
- minute: "2-digit",
389
- second: "2-digit",
426
+ month: "numeric",
427
+ day: "numeric",
428
+ hour: "numeric",
429
+ minute: "numeric",
430
+ second: "numeric",
390
431
  timeZone,
391
432
  hourCycle: "h23"
392
- }).formatToParts(d).map(normStr);
393
- const parts2 = {};
433
+ }).formatToParts(d);
434
+ const p = {};
394
435
  utcParts.forEach((part) => {
395
- parts2[part.type] = part.value;
436
+ if (part.type !== "literal") p[part.type] = part.value;
396
437
  });
397
- return /* @__PURE__ */ new Date(
398
- `${parts2.year}-${parts2.month}-${parts2.day}T${parts2.hour}:${parts2.minute}:${parts2.second}Z`
399
- );
438
+ const year = p.era === "BC" ? 1 - Number(p.year) : Number(p.year);
439
+ const result = new Date(Date.UTC(0, 0, 1, Number(p.hour), Number(p.minute), Number(p.second)));
440
+ result.setUTCFullYear(year, Number(p.month) - 1, Number(p.day));
441
+ return result;
400
442
  }
401
443
  function offset(utcTime, tzA = "UTC", tzB = "device", timeZoneToken = "Z") {
402
444
  var _a;
@@ -1019,12 +1061,61 @@ function sameYear(inputDateA, inputDateB) {
1019
1061
  return a.getFullYear() === b.getFullYear();
1020
1062
  }
1021
1063
 
1064
+ // src/setMilliseconds.ts
1065
+ function setMilliseconds(inputDate, ms) {
1066
+ const d = date(inputDate);
1067
+ d.setMilliseconds(ms);
1068
+ return d;
1069
+ }
1070
+
1071
+ // src/setSeconds.ts
1072
+ function setSeconds(inputDate, second) {
1073
+ const d = date(inputDate);
1074
+ d.setSeconds(second);
1075
+ return d;
1076
+ }
1077
+
1078
+ // src/setMinutes.ts
1079
+ function setMinutes(inputDate, minute) {
1080
+ const d = date(inputDate);
1081
+ d.setMinutes(minute);
1082
+ return d;
1083
+ }
1084
+
1085
+ // src/setHour.ts
1086
+ function setHour(inputDate, hour) {
1087
+ const d = date(inputDate);
1088
+ d.setHours(hour);
1089
+ return d;
1090
+ }
1091
+
1092
+ // src/setDayOfMonth.ts
1093
+ function setDayOfMonth(inputDate, day, dateOverflow = false) {
1094
+ const d = date(inputDate);
1095
+ const daysInMonth = monthDays(d);
1096
+ if (!dateOverflow) {
1097
+ day = day > daysInMonth ? daysInMonth : day;
1098
+ }
1099
+ d.setDate(day);
1100
+ return d;
1101
+ }
1102
+
1103
+ // src/setMonth.ts
1104
+ function setMonth(inputDate, month, dateOverflow = false) {
1105
+ return handleOverflow(inputDate, (d) => d.setMonth(month), dateOverflow);
1106
+ }
1107
+
1108
+ // src/setYear.ts
1109
+ function setYear(inputDate, year, dateOverflow = false) {
1110
+ return handleOverflow(inputDate, (d) => d.setFullYear(year), dateOverflow);
1111
+ }
1112
+
1022
1113
  // src/weekStart.ts
1023
1114
  function weekStart(inputDate, startOfWeekDay = 0) {
1024
1115
  const d = date(inputDate);
1025
- let diff = startOfWeekDay - d.getDay();
1026
- if (diff > 0) diff = diff - 7;
1027
- d.setDate(d.getDate() + diff);
1116
+ let diff2 = startOfWeekDay - d.getDay();
1117
+ if (diff2 > 0) diff2 = diff2 - 7;
1118
+ d.setDate(d.getDate() + diff2);
1028
1119
  d.setHours(0, 0, 0, 0);
1029
1120
  return d;
1030
1121
  }
@@ -1172,7 +1263,76 @@ function diffYears(dateA, dateB) {
1172
1263
  );
1173
1264
  return r == 0 ? 0 : r;
1174
1265
  }
1266
+
1267
+ // src/diff.ts
1268
+ function negateDuration(duration) {
1269
+ const negated = {};
1270
+ for (const unit of Object.keys(duration)) {
1271
+ negated[unit] = -duration[unit];
1272
+ }
1273
+ return negated;
1274
+ }
1275
+ function calendarDiff(current, target, diffUnit, addUnit) {
1276
+ let amount = diffUnit(current, target);
1277
+ let next = addUnit(current, -amount);
1278
+ while (amount > 0 && next < target) {
1279
+ amount--;
1280
+ next = addUnit(current, -amount);
1281
+ }
1282
+ return [amount, next];
1283
+ }
1284
+ function diff(dateA, dateB, options) {
1285
+ let a = date(dateA);
1286
+ let b = date(dateB);
1287
+ if (a < b) {
1288
+ const duration2 = diff(b, a, options);
1289
+ return (options == null ? void 0 : options.abs) ? duration2 : negateDuration(duration2);
1290
+ }
1291
+ const skip = new Set(options == null ? void 0 : options.skip);
1292
+ const duration = {};
1293
+ if (!skip.has("years")) {
1294
+ const [years, next] = calendarDiff(a, b, diffYears, addYear);
1295
+ a = next;
1296
+ if (years) duration.years = years;
1297
+ }
1298
+ if (!skip.has("months")) {
1299
+ const [months, next] = calendarDiff(a, b, diffMonths, addMonth);
1300
+ a = next;
1301
+ if (months) duration.months = months;
1302
+ }
1303
+ if (!skip.has("weeks")) {
1304
+ const weeks = diffWeeks(a, b);
1305
+ a = addDay(a, -(weeks * 7));
1306
+ if (weeks) duration.weeks = weeks;
1307
+ }
1308
+ if (!skip.has("days")) {
1309
+ const days = diffDays(a, b);
1310
+ a = addDay(a, -days);
1311
+ if (days) duration.days = days;
1312
+ }
1313
+ if (!skip.has("hours")) {
1314
+ const hours = diffHours(a, b);
1315
+ a = addHour(a, -hours);
1316
+ if (hours) duration.hours = hours;
1317
+ }
1318
+ if (!skip.has("minutes")) {
1319
+ const minutes = diffMinutes(a, b);
1320
+ a = addMinute(a, -minutes);
1321
+ if (minutes) duration.minutes = minutes;
1322
+ }
1323
+ if (!skip.has("seconds")) {
1324
+ const seconds = diffSeconds(a, b);
1325
+ a = addSecond(a, -seconds);
1326
+ if (seconds) duration.seconds = seconds;
1327
+ }
1328
+ if (!skip.has("milliseconds")) {
1329
+ const ms = diffMilliseconds(a, b);
1330
+ if (ms) duration.milliseconds = ms;
1331
+ }
1332
+ return duration;
1333
+ }
1175
1334
  export {
1335
+ add,
1176
1336
  addDay,
1177
1337
  addHour,
1178
1338
  addMillisecond,
@@ -1186,6 +1346,7 @@ export {
1186
1346
  dayEnd,
1187
1347
  dayOfYear,
1188
1348
  dayStart,
1349
+ diff,
1189
1350
  diffDays,
1190
1351
  diffHours,
1191
1352
  diffMilliseconds,
@@ -1223,6 +1384,13 @@ export {
1223
1384
  sameMinute,
1224
1385
  sameSecond,
1225
1386
  sameYear,
1387
+ setDayOfMonth,
1388
+ setHour,
1389
+ setMilliseconds,
1390
+ setMinutes,
1391
+ setMonth,
1392
+ setSeconds,
1393
+ setYear,
1226
1394
  tzDate,
1227
1395
  weekEnd,
1228
1396
  weekStart,