@formkit/tempo 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) 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 +219 -49
  11. package/dist/bundle.mjs.map +1 -1
  12. package/dist/common.mjs +4 -2
  13. package/dist/common.mjs.map +1 -1
  14. package/dist/diff.d.ts +38 -0
  15. package/dist/diff.mjs +86 -0
  16. package/dist/diff.mjs.map +1 -0
  17. package/dist/handleDateOverflow.d.ts +12 -0
  18. package/dist/handleDateOverflow.mjs +18 -0
  19. package/dist/handleDateOverflow.mjs.map +1 -0
  20. package/dist/index.cjs +228 -49
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +110 -1
  23. package/dist/index.d.ts +10 -1
  24. package/dist/index.mjs +18 -0
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/offset.mjs +14 -12
  27. package/dist/offset.mjs.map +1 -1
  28. package/dist/setDayOfMonth.d.ts +11 -0
  29. package/dist/setDayOfMonth.mjs +16 -0
  30. package/dist/setDayOfMonth.mjs.map +1 -0
  31. package/dist/setHour.d.ts +10 -0
  32. package/dist/setHour.mjs +11 -0
  33. package/dist/setHour.mjs.map +1 -0
  34. package/dist/setMilliseconds.d.ts +10 -0
  35. package/dist/setMilliseconds.mjs +11 -0
  36. package/dist/setMilliseconds.mjs.map +1 -0
  37. package/dist/setMinutes.d.ts +10 -0
  38. package/dist/setMinutes.mjs +11 -0
  39. package/dist/setMinutes.mjs.map +1 -0
  40. package/dist/setMonth.d.ts +11 -0
  41. package/dist/setMonth.mjs +9 -0
  42. package/dist/setMonth.mjs.map +1 -0
  43. package/dist/setSeconds.d.ts +10 -0
  44. package/dist/setSeconds.mjs +11 -0
  45. package/dist/setSeconds.mjs.map +1 -0
  46. package/dist/setYear.d.ts +11 -0
  47. package/dist/setYear.mjs +9 -0
  48. package/dist/setYear.mjs.map +1 -0
  49. package/dist/types.d.ts +15 -1
  50. package/dist/yearDays.mjs +1 -1
  51. package/dist/yearDays.mjs.map +1 -1
  52. 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
 
@@ -185,7 +225,7 @@ var tokens = /* @__PURE__ */ new Map(
185
225
  var dayPeriodMap = /* @__PURE__ */ new Map();
186
226
  var styles = ["full", "long", "medium", "short"];
187
227
  var two = (n) => String(n).padStart(2, "0");
188
- var four = (n) => String(n).padStart(2, "0");
228
+ var four = (n) => String(n).padStart(4, "0");
189
229
  function normStr(part) {
190
230
  if (part.type === "literal") {
191
231
  part.value = part.value.normalize("NFKC");
@@ -230,7 +270,9 @@ function createPartMap(inputDate, parts2, locale, genitive = false) {
230
270
  const valueParts = [];
231
271
  const genitiveParts = [];
232
272
  function addValues(requestedParts, hour122 = false) {
233
- const preciseLocale = `${locale}-u-hc-${hour122 ? "h12" : "h23"}`;
273
+ const preciseLocale = new Intl.Locale(locale, {
274
+ hourCycle: hour122 ? "h12" : "h23"
275
+ }).toString();
234
276
  valueParts.push(
235
277
  ...new Intl.DateTimeFormat(
236
278
  preciseLocale,
@@ -381,22 +423,24 @@ function deviceTZ() {
381
423
  // src/offset.ts
382
424
  function relativeTime(d, timeZone) {
383
425
  const utcParts = new Intl.DateTimeFormat("en-US", {
426
+ era: "short",
384
427
  year: "numeric",
385
- month: "2-digit",
386
- day: "2-digit",
387
- hour: "2-digit",
388
- minute: "2-digit",
389
- second: "2-digit",
428
+ month: "numeric",
429
+ day: "numeric",
430
+ hour: "numeric",
431
+ minute: "numeric",
432
+ second: "numeric",
390
433
  timeZone,
391
434
  hourCycle: "h23"
392
- }).formatToParts(d).map(normStr);
393
- const parts2 = {};
435
+ }).formatToParts(d);
436
+ const p = {};
394
437
  utcParts.forEach((part) => {
395
- parts2[part.type] = part.value;
438
+ if (part.type !== "literal") p[part.type] = part.value;
396
439
  });
397
- return /* @__PURE__ */ new Date(
398
- `${parts2.year}-${parts2.month}-${parts2.day}T${parts2.hour}:${parts2.minute}:${parts2.second}Z`
399
- );
440
+ const year = p.era === "BC" ? 1 - Number(p.year) : Number(p.year);
441
+ const result = new Date(Date.UTC(0, 0, 1, Number(p.hour), Number(p.minute), Number(p.second)));
442
+ result.setUTCFullYear(year, Number(p.month) - 1, Number(p.day));
443
+ return result;
400
444
  }
401
445
  function offset(utcTime, tzA = "UTC", tzB = "device", timeZoneToken = "Z") {
402
446
  var _a;
@@ -733,7 +777,7 @@ function monthStart(inputDate) {
733
777
  // src/yearDays.ts
734
778
  function yearDays(inputDate) {
735
779
  const d = date(inputDate);
736
- return (new Date(d.getFullYear() + 1, 0, 0).getTime() - new Date(d.getFullYear(), 0, 0).getTime()) / 864e5;
780
+ return (Date.UTC(d.getFullYear() + 1, 0, 0) - Date.UTC(d.getFullYear(), 0, 0)) / 864e5;
737
781
  }
738
782
 
739
783
  // src/nearestDay.ts
@@ -1019,12 +1063,61 @@ function sameYear(inputDateA, inputDateB) {
1019
1063
  return a.getFullYear() === b.getFullYear();
1020
1064
  }
1021
1065
 
1066
+ // src/setMilliseconds.ts
1067
+ function setMilliseconds(inputDate, ms) {
1068
+ const d = date(inputDate);
1069
+ d.setMilliseconds(ms);
1070
+ return d;
1071
+ }
1072
+
1073
+ // src/setSeconds.ts
1074
+ function setSeconds(inputDate, second) {
1075
+ const d = date(inputDate);
1076
+ d.setSeconds(second);
1077
+ return d;
1078
+ }
1079
+
1080
+ // src/setMinutes.ts
1081
+ function setMinutes(inputDate, minute) {
1082
+ const d = date(inputDate);
1083
+ d.setMinutes(minute);
1084
+ return d;
1085
+ }
1086
+
1087
+ // src/setHour.ts
1088
+ function setHour(inputDate, hour) {
1089
+ const d = date(inputDate);
1090
+ d.setHours(hour);
1091
+ return d;
1092
+ }
1093
+
1094
+ // src/setDayOfMonth.ts
1095
+ function setDayOfMonth(inputDate, day, dateOverflow = false) {
1096
+ const d = date(inputDate);
1097
+ const daysInMonth = monthDays(d);
1098
+ if (!dateOverflow) {
1099
+ day = day > daysInMonth ? daysInMonth : day;
1100
+ }
1101
+ d.setDate(day);
1102
+ return d;
1103
+ }
1104
+
1105
+ // src/setMonth.ts
1106
+ function setMonth(inputDate, month, dateOverflow = false) {
1107
+ return handleOverflow(inputDate, (d) => d.setMonth(month), dateOverflow);
1108
+ }
1109
+
1110
+ // src/setYear.ts
1111
+ function setYear(inputDate, year, dateOverflow = false) {
1112
+ return handleOverflow(inputDate, (d) => d.setFullYear(year), dateOverflow);
1113
+ }
1114
+
1022
1115
  // src/weekStart.ts
1023
1116
  function weekStart(inputDate, startOfWeekDay = 0) {
1024
1117
  const d = date(inputDate);
1025
- let diff = startOfWeekDay - d.getDay();
1026
- if (diff > 0) diff = diff - 7;
1027
- d.setDate(d.getDate() + diff);
1118
+ let diff2 = startOfWeekDay - d.getDay();
1119
+ if (diff2 > 0) diff2 = diff2 - 7;
1120
+ d.setDate(d.getDate() + diff2);
1028
1121
  d.setHours(0, 0, 0, 0);
1029
1122
  return d;
1030
1123
  }
@@ -1172,7 +1265,76 @@ function diffYears(dateA, dateB) {
1172
1265
  );
1173
1266
  return r == 0 ? 0 : r;
1174
1267
  }
1268
+
1269
+ // src/diff.ts
1270
+ function negateDuration(duration) {
1271
+ const negated = {};
1272
+ for (const unit of Object.keys(duration)) {
1273
+ negated[unit] = -duration[unit];
1274
+ }
1275
+ return negated;
1276
+ }
1277
+ function calendarDiff(current, target, diffUnit, addUnit) {
1278
+ let amount = diffUnit(current, target);
1279
+ let next = addUnit(current, -amount);
1280
+ while (amount > 0 && next < target) {
1281
+ amount--;
1282
+ next = addUnit(current, -amount);
1283
+ }
1284
+ return [amount, next];
1285
+ }
1286
+ function diff(dateA, dateB, options) {
1287
+ let a = date(dateA);
1288
+ let b = date(dateB);
1289
+ if (a < b) {
1290
+ const duration2 = diff(b, a, options);
1291
+ return (options == null ? void 0 : options.abs) ? duration2 : negateDuration(duration2);
1292
+ }
1293
+ const skip = new Set(options == null ? void 0 : options.skip);
1294
+ const duration = {};
1295
+ if (!skip.has("years")) {
1296
+ const [years, next] = calendarDiff(a, b, diffYears, addYear);
1297
+ a = next;
1298
+ if (years) duration.years = years;
1299
+ }
1300
+ if (!skip.has("months")) {
1301
+ const [months, next] = calendarDiff(a, b, diffMonths, addMonth);
1302
+ a = next;
1303
+ if (months) duration.months = months;
1304
+ }
1305
+ if (!skip.has("weeks")) {
1306
+ const weeks = diffWeeks(a, b);
1307
+ a = addDay(a, -(weeks * 7));
1308
+ if (weeks) duration.weeks = weeks;
1309
+ }
1310
+ if (!skip.has("days")) {
1311
+ const days = diffDays(a, b);
1312
+ a = addDay(a, -days);
1313
+ if (days) duration.days = days;
1314
+ }
1315
+ if (!skip.has("hours")) {
1316
+ const hours = diffHours(a, b);
1317
+ a = addHour(a, -hours);
1318
+ if (hours) duration.hours = hours;
1319
+ }
1320
+ if (!skip.has("minutes")) {
1321
+ const minutes = diffMinutes(a, b);
1322
+ a = addMinute(a, -minutes);
1323
+ if (minutes) duration.minutes = minutes;
1324
+ }
1325
+ if (!skip.has("seconds")) {
1326
+ const seconds = diffSeconds(a, b);
1327
+ a = addSecond(a, -seconds);
1328
+ if (seconds) duration.seconds = seconds;
1329
+ }
1330
+ if (!skip.has("milliseconds")) {
1331
+ const ms = diffMilliseconds(a, b);
1332
+ if (ms) duration.milliseconds = ms;
1333
+ }
1334
+ return duration;
1335
+ }
1175
1336
  export {
1337
+ add,
1176
1338
  addDay,
1177
1339
  addHour,
1178
1340
  addMillisecond,
@@ -1186,6 +1348,7 @@ export {
1186
1348
  dayEnd,
1187
1349
  dayOfYear,
1188
1350
  dayStart,
1351
+ diff,
1189
1352
  diffDays,
1190
1353
  diffHours,
1191
1354
  diffMilliseconds,
@@ -1223,6 +1386,13 @@ export {
1223
1386
  sameMinute,
1224
1387
  sameSecond,
1225
1388
  sameYear,
1389
+ setDayOfMonth,
1390
+ setHour,
1391
+ setMilliseconds,
1392
+ setMinutes,
1393
+ setMonth,
1394
+ setSeconds,
1395
+ setYear,
1226
1396
  tzDate,
1227
1397
  weekEnd,
1228
1398
  weekStart,