temporal-utils 1.0.0 → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +873 -0
- package/dist/diff.d.ts +17 -0
- package/dist/diff.js +52 -0
- package/dist/endOf.d.ts +9 -0
- package/dist/endOf.js +56 -0
- package/dist/errorMessages.d.ts +9 -0
- package/dist/errorMessages.js +14 -0
- package/dist/global-types.d.ts +1 -0
- package/dist/global-types.js +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/protected-error-messages.d.ts +1 -0
- package/dist/protected-error-messages.js +1 -0
- package/dist/protected.d.ts +1 -0
- package/dist/protected.js +1 -0
- package/dist/round.d.ts +24 -0
- package/dist/round.js +47 -0
- package/dist/startOf.d.ts +9 -0
- package/dist/startOf.js +73 -0
- package/dist/utils.d.ts +20 -0
- package/dist/utils.js +81 -0
- package/dist/with.d.ts +3 -0
- package/dist/with.js +44 -0
- package/package.json +54 -8
package/README.md
ADDED
|
@@ -0,0 +1,873 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Links here are NOT rewritten on publish. Any link to another file in this repo
|
|
3
|
+
must be an absolute GitHub URL rooted at:
|
|
4
|
+
https://github.com/fullcalendar/temporal-polyfill/blob/main/
|
|
5
|
+
(use /tree/main/ instead of /blob/main/ when linking a directory)
|
|
6
|
+
-->
|
|
7
|
+
|
|
8
|
+
# temporal-utils
|
|
9
|
+
|
|
10
|
+
Small, application-level helpers for working with native [Temporal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal) objects
|
|
11
|
+
|
|
12
|
+
Most helpers preserve the input type. For example, passing a
|
|
13
|
+
`Temporal.PlainDateTime` to `startOfMonth` returns a `Temporal.PlainDateTime`,
|
|
14
|
+
while passing a `Temporal.ZonedDateTime` returns a `Temporal.ZonedDateTime`.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm install temporal-utils
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`temporal-utils` does not ship a Temporal implementation of its own — a global
|
|
23
|
+
`Temporal` must already exist at runtime. See [Requirements](#requirements).
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Import the helpers you need from the package root and call them on native
|
|
28
|
+
Temporal objects. Each helper accepts a range of Temporal types and gives you
|
|
29
|
+
back the same type you passed in:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { startOfMonth, diffDays } from 'temporal-utils'
|
|
33
|
+
|
|
34
|
+
// Pass a PlainDate, get a PlainDate back
|
|
35
|
+
startOfMonth(Temporal.PlainDate.from('2024-07-20')).toString()
|
|
36
|
+
// '2024-07-01'
|
|
37
|
+
|
|
38
|
+
// Pass a ZonedDateTime, get a ZonedDateTime back
|
|
39
|
+
startOfMonth(
|
|
40
|
+
Temporal.ZonedDateTime.from('2024-07-20T12:30[America/New_York]'),
|
|
41
|
+
).toString()
|
|
42
|
+
// '2024-07-01T00:00:00-04:00[America/New_York]'
|
|
43
|
+
|
|
44
|
+
diffDays(
|
|
45
|
+
Temporal.PlainDate.from('2024-07-01'),
|
|
46
|
+
Temporal.PlainDate.from('2024-07-20'),
|
|
47
|
+
)
|
|
48
|
+
// 19
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Requirements
|
|
52
|
+
|
|
53
|
+
`temporal-utils` operates on native `Temporal` objects but does **not** include
|
|
54
|
+
a Temporal implementation of its own. A global `Temporal` must already exist at
|
|
55
|
+
runtime — note that every example below references `Temporal.*` directly to
|
|
56
|
+
build its inputs. Ensure one is present in one of two ways:
|
|
57
|
+
|
|
58
|
+
- **Native `Temporal`** — available in newer browsers and runtimes, but support
|
|
59
|
+
is still rolling out and remains incomplete.
|
|
60
|
+
- **A polyfill** — recommended for now. We suggest
|
|
61
|
+
[`temporal-polyfill`](https://github.com/fullcalendar/temporal-polyfill/blob/main/polyfill/README.md), which installs a global
|
|
62
|
+
`Temporal`:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import 'temporal-polyfill/global'
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Function Reference
|
|
69
|
+
|
|
70
|
+
- [Field Replacement](#field-replacement)
|
|
71
|
+
- [`withDayOfYear`](#withdayofyear)
|
|
72
|
+
- [`withDayOfWeek`](#withdayofweek)
|
|
73
|
+
- [`withWeekOfYear`](#withweekofyear)
|
|
74
|
+
- [Difference Helpers](#difference-helpers)
|
|
75
|
+
- [`diffYears`](#diffyears)
|
|
76
|
+
- [`diffMonths`](#diffmonths)
|
|
77
|
+
- [`diffWeeks`](#diffweeks)
|
|
78
|
+
- [`diffDays`](#diffdays)
|
|
79
|
+
- [`diffHours`](#diffhours)
|
|
80
|
+
- [`diffMinutes`](#diffminutes)
|
|
81
|
+
- [`diffSeconds`](#diffseconds)
|
|
82
|
+
- [`diffMilliseconds`](#diffmilliseconds)
|
|
83
|
+
- [`diffMicroseconds`](#diffmicroseconds)
|
|
84
|
+
- [`diffNanoseconds`](#diffnanoseconds)
|
|
85
|
+
- [Rounding Helpers](#rounding-helpers)
|
|
86
|
+
- [`roundToYear`](#roundtoyear)
|
|
87
|
+
- [`roundToMonth`](#roundtomonth)
|
|
88
|
+
- [`roundToWeek`](#roundtoweek)
|
|
89
|
+
- [⚠️ `roundToDay`](#roundtoday)
|
|
90
|
+
- [⚠️ `roundToHour`](#roundtohour)
|
|
91
|
+
- [⚠️ `roundToMinute`](#roundtominute)
|
|
92
|
+
- [⚠️ `roundToSecond`](#roundtosecond)
|
|
93
|
+
- [⚠️ `roundToMillisecond`](#roundtomillisecond)
|
|
94
|
+
- [⚠️ `roundToMicrosecond`](#roundtomicrosecond)
|
|
95
|
+
- [Start Of Unit](#start-of-unit)
|
|
96
|
+
- [`startOfYear`](#startofyear)
|
|
97
|
+
- [`startOfMonth`](#startofmonth)
|
|
98
|
+
- [`startOfWeek`](#startofweek)
|
|
99
|
+
- [`startOfDay`](#startofday)
|
|
100
|
+
- [`startOfHour`](#startofhour)
|
|
101
|
+
- [`startOfMinute`](#startofminute)
|
|
102
|
+
- [`startOfSecond`](#startofsecond)
|
|
103
|
+
- [`startOfMillisecond`](#startofmillisecond)
|
|
104
|
+
- [`startOfMicrosecond`](#startofmicrosecond)
|
|
105
|
+
- [End Of Unit](#end-of-unit)
|
|
106
|
+
- [`endOfYear`](#endofyear)
|
|
107
|
+
- [`endOfMonth`](#endofmonth)
|
|
108
|
+
- [`endOfWeek`](#endofweek)
|
|
109
|
+
- [`endOfDay`](#endofday)
|
|
110
|
+
- [`endOfHour`](#endofhour)
|
|
111
|
+
- [`endOfMinute`](#endofminute)
|
|
112
|
+
- [`endOfSecond`](#endofsecond)
|
|
113
|
+
- [`endOfMillisecond`](#endofmillisecond)
|
|
114
|
+
- [`endOfMicrosecond`](#endofmicrosecond)
|
|
115
|
+
- [Shared Types](#shared-types)
|
|
116
|
+
- [`RoundingMode`](#roundingmode)
|
|
117
|
+
- [`RoundingMathOptions`](#roundingmathoptions)
|
|
118
|
+
|
|
119
|
+
## Field Replacement
|
|
120
|
+
|
|
121
|
+
These helpers fill gaps where Temporal exposes calendar-derived fields such as
|
|
122
|
+
`dayOfYear`, `dayOfWeek`, and `weekOfYear`, but does not provide direct
|
|
123
|
+
`with({ ... })` fields for replacing them.
|
|
124
|
+
|
|
125
|
+
All field replacement helpers:
|
|
126
|
+
|
|
127
|
+
- accept `Temporal.PlainDate`, `Temporal.PlainDateTime`, or
|
|
128
|
+
`Temporal.ZonedDateTime`
|
|
129
|
+
- truncate numeric input toward zero before applying it
|
|
130
|
+
- accept Temporal field overflow options: `{ overflow: 'constrain' }` or
|
|
131
|
+
`{ overflow: 'reject' }`
|
|
132
|
+
- default overflow behavior to `'constrain'`
|
|
133
|
+
|
|
134
|
+
### `withDayOfYear`
|
|
135
|
+
|
|
136
|
+
Signature:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
function withDayOfYear<
|
|
140
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
141
|
+
>(
|
|
142
|
+
date: T,
|
|
143
|
+
dayOfYear: number,
|
|
144
|
+
options?: Temporal.OverflowOptions,
|
|
145
|
+
): T
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Returns the same calendar year with `dayOfYear` replaced. The time, time zone,
|
|
149
|
+
and calendar are preserved.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { withDayOfYear } from 'temporal-utils'
|
|
153
|
+
|
|
154
|
+
const date = Temporal.PlainDate.from('2024-02-27')
|
|
155
|
+
|
|
156
|
+
withDayOfYear(date, 5).toString()
|
|
157
|
+
// '2024-01-05'
|
|
158
|
+
|
|
159
|
+
withDayOfYear(date, 500).toString()
|
|
160
|
+
// '2024-12-31'
|
|
161
|
+
|
|
162
|
+
withDayOfYear(date, 500, { overflow: 'reject' })
|
|
163
|
+
// RangeError
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### `withDayOfWeek`
|
|
167
|
+
|
|
168
|
+
Signature:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
function withDayOfWeek<
|
|
172
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
173
|
+
>(
|
|
174
|
+
date: T,
|
|
175
|
+
dayOfWeek: number,
|
|
176
|
+
options?: Temporal.OverflowOptions,
|
|
177
|
+
): T
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Returns the same ISO week with `dayOfWeek` replaced. Temporal uses ISO weekday
|
|
181
|
+
numbers: Monday is `1` and Sunday is `7`.
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { withDayOfWeek } from 'temporal-utils'
|
|
185
|
+
|
|
186
|
+
const date = Temporal.PlainDate.from('2024-02-27') // Tuesday
|
|
187
|
+
|
|
188
|
+
withDayOfWeek(date, 4).toString()
|
|
189
|
+
// '2024-02-29'
|
|
190
|
+
|
|
191
|
+
withDayOfWeek(date, 8).toString()
|
|
192
|
+
// '2024-03-03'
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### `withWeekOfYear`
|
|
196
|
+
|
|
197
|
+
Signature:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
function withWeekOfYear<
|
|
201
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
202
|
+
>(
|
|
203
|
+
date: T,
|
|
204
|
+
weekOfYear: number,
|
|
205
|
+
options?: Temporal.OverflowOptions,
|
|
206
|
+
): T
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Returns the same ISO week-year with `weekOfYear` replaced, preserving the
|
|
210
|
+
current day of week. This helper is ISO-only and throws a `RangeError` for
|
|
211
|
+
calendars where Temporal does not expose ISO week fields.
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
import { withWeekOfYear } from 'temporal-utils'
|
|
215
|
+
|
|
216
|
+
const date = Temporal.PlainDate.from('2024-02-27') // Tuesday, week 9
|
|
217
|
+
|
|
218
|
+
withWeekOfYear(date, 27).toString()
|
|
219
|
+
// '2024-07-02'
|
|
220
|
+
|
|
221
|
+
withWeekOfYear(date.withCalendar('hebrew'), 27)
|
|
222
|
+
// RangeError
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Difference Helpers
|
|
226
|
+
|
|
227
|
+
Single-unit difference helpers return a number for `date0.until(date1)`. The
|
|
228
|
+
helpers are directional: reversing the arguments reverses the sign.
|
|
229
|
+
|
|
230
|
+
If no options are supplied, the result is the exact total in the requested unit.
|
|
231
|
+
Pass a `RoundingMode` string or a `RoundingMathOptions` object to request Temporal
|
|
232
|
+
rounding behavior for that unit.
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { diffDays, diffMonths } from 'temporal-utils'
|
|
236
|
+
|
|
237
|
+
const start = Temporal.PlainDate.from('2024-02-20')
|
|
238
|
+
const end = Temporal.PlainDate.from('2024-04-10')
|
|
239
|
+
|
|
240
|
+
diffMonths(start, end)
|
|
241
|
+
// approximately 1.677...
|
|
242
|
+
|
|
243
|
+
diffMonths(start, end, 'floor')
|
|
244
|
+
// 1
|
|
245
|
+
|
|
246
|
+
diffDays(start, end, { roundingMode: 'ceil', roundingIncrement: 7 })
|
|
247
|
+
// 56
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Date and calendar unit helpers use `relativeTo` internally so totals account
|
|
251
|
+
for calendar units. For `Temporal.ZonedDateTime`, totals also account for
|
|
252
|
+
time-zone transitions. Time-unit helpers produce elapsed-time totals.
|
|
253
|
+
|
|
254
|
+
### `diffYears`
|
|
255
|
+
|
|
256
|
+
Signature:
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
function diffYears(
|
|
260
|
+
date0: Temporal.PlainYearMonth | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
261
|
+
date1: Temporal.PlainYearMonth | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
262
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
263
|
+
): number
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Returns the difference in years. Supported input types are
|
|
267
|
+
`Temporal.PlainYearMonth`, `Temporal.PlainDate`, `Temporal.PlainDateTime`, and
|
|
268
|
+
`Temporal.ZonedDateTime`.
|
|
269
|
+
|
|
270
|
+
### `diffMonths`
|
|
271
|
+
|
|
272
|
+
Signature:
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
function diffMonths(
|
|
276
|
+
date0: Temporal.PlainYearMonth | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
277
|
+
date1: Temporal.PlainYearMonth | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
278
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
279
|
+
): number
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Returns the difference in months. Supported input types are
|
|
283
|
+
`Temporal.PlainYearMonth`, `Temporal.PlainDate`, `Temporal.PlainDateTime`, and
|
|
284
|
+
`Temporal.ZonedDateTime`.
|
|
285
|
+
|
|
286
|
+
### `diffWeeks`
|
|
287
|
+
|
|
288
|
+
Signature:
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
function diffWeeks(
|
|
292
|
+
date0: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
293
|
+
date1: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
294
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
295
|
+
): number
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
Returns the difference in weeks. Supported input types are `Temporal.PlainDate`,
|
|
299
|
+
`Temporal.PlainDateTime`, and `Temporal.ZonedDateTime`.
|
|
300
|
+
|
|
301
|
+
### `diffDays`
|
|
302
|
+
|
|
303
|
+
Signature:
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
function diffDays(
|
|
307
|
+
date0: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
308
|
+
date1: Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
309
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
310
|
+
): number
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Returns the difference in days. Supported input types are `Temporal.PlainDate`,
|
|
314
|
+
`Temporal.PlainDateTime`, and `Temporal.ZonedDateTime`.
|
|
315
|
+
|
|
316
|
+
### `diffHours`
|
|
317
|
+
|
|
318
|
+
Signature:
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
function diffHours(
|
|
322
|
+
date0: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
323
|
+
date1: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
324
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
325
|
+
): number
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Returns the difference in hours. Supported input types are `Temporal.Instant`,
|
|
329
|
+
`Temporal.PlainTime`, `Temporal.PlainDateTime`, and `Temporal.ZonedDateTime`.
|
|
330
|
+
|
|
331
|
+
### `diffMinutes`
|
|
332
|
+
|
|
333
|
+
Signature:
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
function diffMinutes(
|
|
337
|
+
date0: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
338
|
+
date1: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
339
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
340
|
+
): number
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
Returns the difference in minutes. Supported input types are `Temporal.Instant`,
|
|
344
|
+
`Temporal.PlainTime`, `Temporal.PlainDateTime`, and `Temporal.ZonedDateTime`.
|
|
345
|
+
|
|
346
|
+
### `diffSeconds`
|
|
347
|
+
|
|
348
|
+
Signature:
|
|
349
|
+
|
|
350
|
+
```ts
|
|
351
|
+
function diffSeconds(
|
|
352
|
+
date0: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
353
|
+
date1: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
354
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
355
|
+
): number
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Returns the difference in seconds. Supported input types are `Temporal.Instant`,
|
|
359
|
+
`Temporal.PlainTime`, `Temporal.PlainDateTime`, and `Temporal.ZonedDateTime`.
|
|
360
|
+
|
|
361
|
+
### `diffMilliseconds`
|
|
362
|
+
|
|
363
|
+
Signature:
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
function diffMilliseconds(
|
|
367
|
+
date0: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
368
|
+
date1: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
369
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
370
|
+
): number
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
Returns the difference in milliseconds. Supported input types are
|
|
374
|
+
`Temporal.Instant`, `Temporal.PlainTime`, `Temporal.PlainDateTime`, and
|
|
375
|
+
`Temporal.ZonedDateTime`.
|
|
376
|
+
|
|
377
|
+
### `diffMicroseconds`
|
|
378
|
+
|
|
379
|
+
Signature:
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
function diffMicroseconds(
|
|
383
|
+
date0: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
384
|
+
date1: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
385
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
386
|
+
): number
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
Returns the difference in microseconds. Supported input types are
|
|
390
|
+
`Temporal.Instant`, `Temporal.PlainTime`, `Temporal.PlainDateTime`, and
|
|
391
|
+
`Temporal.ZonedDateTime`.
|
|
392
|
+
|
|
393
|
+
### `diffNanoseconds`
|
|
394
|
+
|
|
395
|
+
Signature:
|
|
396
|
+
|
|
397
|
+
```ts
|
|
398
|
+
function diffNanoseconds(
|
|
399
|
+
date0: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
400
|
+
date1: Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
401
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
402
|
+
): number
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
Returns the difference in nanoseconds. Supported input types are
|
|
406
|
+
`Temporal.Instant`, `Temporal.PlainTime`, `Temporal.PlainDateTime`, and
|
|
407
|
+
`Temporal.ZonedDateTime`.
|
|
408
|
+
|
|
409
|
+
## Rounding Helpers
|
|
410
|
+
|
|
411
|
+
Calendar rounding helpers return the closest start of the named unit. They are
|
|
412
|
+
useful because Temporal's native `.round()` APIs cover day and smaller units,
|
|
413
|
+
but not every larger calendar unit in a uniform way across date-like types.
|
|
414
|
+
|
|
415
|
+
If `options` or `options.roundingMode` is omitted, `roundingMode` defaults to
|
|
416
|
+
`'halfExpand'`. Pass a `RoundingMode` string as shorthand for
|
|
417
|
+
`{ roundingMode }`.
|
|
418
|
+
|
|
419
|
+
`RoundingMathOptions` must not include `smallestUnit`; the helper name supplies
|
|
420
|
+
the unit. For the year, month, and week helpers, `roundingIncrement` must be
|
|
421
|
+
omitted or `1`.
|
|
422
|
+
|
|
423
|
+
⚠️ The day and smaller `roundTo*` helpers are exported as codemod targets, not
|
|
424
|
+
as recommended hand-written API. In application code, prefer native Temporal
|
|
425
|
+
`.round()` directly:
|
|
426
|
+
|
|
427
|
+
```ts
|
|
428
|
+
dateTime.round({ smallestUnit: 'day' })
|
|
429
|
+
time.round({ smallestUnit: 'minute', roundingMode: 'ceil' })
|
|
430
|
+
instant.round({ smallestUnit: 'second' })
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
```ts
|
|
434
|
+
import { roundToMonth, roundToWeek, roundToYear } from 'temporal-utils'
|
|
435
|
+
|
|
436
|
+
const dateTime = Temporal.PlainDateTime.from('2024-07-20T12:30:00')
|
|
437
|
+
|
|
438
|
+
roundToYear(dateTime).toString()
|
|
439
|
+
// '2025-01-01T00:00:00'
|
|
440
|
+
|
|
441
|
+
roundToMonth(dateTime, 'floor').toString()
|
|
442
|
+
// '2024-07-01T00:00:00'
|
|
443
|
+
|
|
444
|
+
roundToWeek(dateTime, { roundingMode: 'floor' }).toString()
|
|
445
|
+
// '2024-07-15T00:00:00'
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### `roundToYear`
|
|
449
|
+
|
|
450
|
+
Signature:
|
|
451
|
+
|
|
452
|
+
```ts
|
|
453
|
+
function roundToYear<
|
|
454
|
+
T extends
|
|
455
|
+
| Temporal.PlainYearMonth
|
|
456
|
+
| Temporal.PlainDate
|
|
457
|
+
| Temporal.PlainDateTime
|
|
458
|
+
| Temporal.ZonedDateTime,
|
|
459
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
Rounds to the nearest year boundary. Supported input types are
|
|
463
|
+
`Temporal.PlainYearMonth`, `Temporal.PlainDate`, `Temporal.PlainDateTime`, and
|
|
464
|
+
`Temporal.ZonedDateTime`.
|
|
465
|
+
|
|
466
|
+
### `roundToMonth`
|
|
467
|
+
|
|
468
|
+
Signature:
|
|
469
|
+
|
|
470
|
+
```ts
|
|
471
|
+
function roundToMonth<
|
|
472
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
473
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
Rounds to the nearest month boundary. Supported input types are
|
|
477
|
+
`Temporal.PlainDate`, `Temporal.PlainDateTime`, and `Temporal.ZonedDateTime`.
|
|
478
|
+
|
|
479
|
+
### `roundToWeek`
|
|
480
|
+
|
|
481
|
+
Signature:
|
|
482
|
+
|
|
483
|
+
```ts
|
|
484
|
+
function roundToWeek<
|
|
485
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
486
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
Rounds to the nearest ISO week boundary. Supported input types are
|
|
490
|
+
`Temporal.PlainDate`, `Temporal.PlainDateTime`, and `Temporal.ZonedDateTime`.
|
|
491
|
+
|
|
492
|
+
<a id="roundtoday"></a>
|
|
493
|
+
|
|
494
|
+
### ⚠️ `roundToDay`
|
|
495
|
+
|
|
496
|
+
Signature:
|
|
497
|
+
|
|
498
|
+
```ts
|
|
499
|
+
function roundToDay<T extends Temporal.PlainDateTime | Temporal.ZonedDateTime>(
|
|
500
|
+
date: T,
|
|
501
|
+
options?: RoundingMode | RoundingMathOptions,
|
|
502
|
+
): T
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
Codemod target for rounding to the nearest day. Prefer
|
|
506
|
+
`dateTime.round({ smallestUnit: 'day' })` in hand-written code.
|
|
507
|
+
|
|
508
|
+
<a id="roundtohour"></a>
|
|
509
|
+
|
|
510
|
+
### ⚠️ `roundToHour`
|
|
511
|
+
|
|
512
|
+
Signature:
|
|
513
|
+
|
|
514
|
+
```ts
|
|
515
|
+
function roundToHour<
|
|
516
|
+
T extends Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
517
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
Codemod target for rounding to the nearest hour. Prefer
|
|
521
|
+
`value.round({ smallestUnit: 'hour' })` in hand-written code.
|
|
522
|
+
|
|
523
|
+
<a id="roundtominute"></a>
|
|
524
|
+
|
|
525
|
+
### ⚠️ `roundToMinute`
|
|
526
|
+
|
|
527
|
+
Signature:
|
|
528
|
+
|
|
529
|
+
```ts
|
|
530
|
+
function roundToMinute<
|
|
531
|
+
T extends Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
532
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
Codemod target for rounding to the nearest minute. Prefer
|
|
536
|
+
`value.round({ smallestUnit: 'minute' })` in hand-written code.
|
|
537
|
+
|
|
538
|
+
<a id="roundtosecond"></a>
|
|
539
|
+
|
|
540
|
+
### ⚠️ `roundToSecond`
|
|
541
|
+
|
|
542
|
+
Signature:
|
|
543
|
+
|
|
544
|
+
```ts
|
|
545
|
+
function roundToSecond<
|
|
546
|
+
T extends Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
547
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
Codemod target for rounding to the nearest second. Prefer
|
|
551
|
+
`value.round({ smallestUnit: 'second' })` in hand-written code.
|
|
552
|
+
|
|
553
|
+
<a id="roundtomillisecond"></a>
|
|
554
|
+
|
|
555
|
+
### ⚠️ `roundToMillisecond`
|
|
556
|
+
|
|
557
|
+
Signature:
|
|
558
|
+
|
|
559
|
+
```ts
|
|
560
|
+
function roundToMillisecond<
|
|
561
|
+
T extends Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
562
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
Codemod target for rounding to the nearest millisecond. Prefer
|
|
566
|
+
`value.round({ smallestUnit: 'millisecond' })` in hand-written code.
|
|
567
|
+
|
|
568
|
+
<a id="roundtomicrosecond"></a>
|
|
569
|
+
|
|
570
|
+
### ⚠️ `roundToMicrosecond`
|
|
571
|
+
|
|
572
|
+
Signature:
|
|
573
|
+
|
|
574
|
+
```ts
|
|
575
|
+
function roundToMicrosecond<
|
|
576
|
+
T extends Temporal.Instant | Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
577
|
+
>(date: T, options?: RoundingMode | RoundingMathOptions): T
|
|
578
|
+
```
|
|
579
|
+
|
|
580
|
+
Codemod target for rounding to the nearest microsecond. Prefer
|
|
581
|
+
`value.round({ smallestUnit: 'microsecond' })` in hand-written code.
|
|
582
|
+
|
|
583
|
+
## Start Of Unit
|
|
584
|
+
|
|
585
|
+
Start helpers truncate smaller fields to the beginning of the named unit.
|
|
586
|
+
`startOfWeek` uses ISO weeks, so the week starts on Monday.
|
|
587
|
+
|
|
588
|
+
For `Temporal.ZonedDateTime`, these helpers use Temporal's own field
|
|
589
|
+
replacement behavior. If local midnight is skipped by a time-zone transition,
|
|
590
|
+
Temporal resolves to the first real instant after the skipped wall time.
|
|
591
|
+
|
|
592
|
+
```ts
|
|
593
|
+
import { startOfMonth, startOfWeek } from 'temporal-utils'
|
|
594
|
+
|
|
595
|
+
const zdt = Temporal.ZonedDateTime.from('2024-07-20T12:30:00[America/New_York]')
|
|
596
|
+
|
|
597
|
+
startOfMonth(zdt).toString()
|
|
598
|
+
// '2024-07-01T00:00:00-04:00[America/New_York]'
|
|
599
|
+
|
|
600
|
+
startOfWeek(zdt).toString()
|
|
601
|
+
// '2024-07-15T00:00:00-04:00[America/New_York]'
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
### `startOfYear`
|
|
605
|
+
|
|
606
|
+
Signature:
|
|
607
|
+
|
|
608
|
+
```ts
|
|
609
|
+
function startOfYear<
|
|
610
|
+
T extends
|
|
611
|
+
| Temporal.PlainYearMonth
|
|
612
|
+
| Temporal.PlainDate
|
|
613
|
+
| Temporal.PlainDateTime
|
|
614
|
+
| Temporal.ZonedDateTime,
|
|
615
|
+
>(date: T): T
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
Returns the start of the current year. For `Temporal.PlainYearMonth`, this is
|
|
619
|
+
January of the same year.
|
|
620
|
+
|
|
621
|
+
### `startOfMonth`
|
|
622
|
+
|
|
623
|
+
Signature:
|
|
624
|
+
|
|
625
|
+
```ts
|
|
626
|
+
function startOfMonth<
|
|
627
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
628
|
+
>(date: T): T
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
Returns the start of the current month.
|
|
632
|
+
|
|
633
|
+
### `startOfWeek`
|
|
634
|
+
|
|
635
|
+
Signature:
|
|
636
|
+
|
|
637
|
+
```ts
|
|
638
|
+
function startOfWeek<
|
|
639
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
640
|
+
>(date: T): T
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
Returns the start of the current ISO week, which is Monday.
|
|
644
|
+
|
|
645
|
+
### `startOfDay`
|
|
646
|
+
|
|
647
|
+
Signature:
|
|
648
|
+
|
|
649
|
+
```ts
|
|
650
|
+
function startOfDay<T extends Temporal.PlainDateTime | Temporal.ZonedDateTime>(
|
|
651
|
+
dateTime: T,
|
|
652
|
+
): T
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
Returns midnight at the start of the current day.
|
|
656
|
+
|
|
657
|
+
### `startOfHour`
|
|
658
|
+
|
|
659
|
+
Signature:
|
|
660
|
+
|
|
661
|
+
```ts
|
|
662
|
+
function startOfHour<
|
|
663
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
664
|
+
>(dateTime: T): T
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
Returns the start of the current hour.
|
|
668
|
+
|
|
669
|
+
### `startOfMinute`
|
|
670
|
+
|
|
671
|
+
Signature:
|
|
672
|
+
|
|
673
|
+
```ts
|
|
674
|
+
function startOfMinute<
|
|
675
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
676
|
+
>(dateTime: T): T
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
Returns the start of the current minute.
|
|
680
|
+
|
|
681
|
+
### `startOfSecond`
|
|
682
|
+
|
|
683
|
+
Signature:
|
|
684
|
+
|
|
685
|
+
```ts
|
|
686
|
+
function startOfSecond<
|
|
687
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
688
|
+
>(dateTime: T): T
|
|
689
|
+
```
|
|
690
|
+
|
|
691
|
+
Returns the start of the current second.
|
|
692
|
+
|
|
693
|
+
### `startOfMillisecond`
|
|
694
|
+
|
|
695
|
+
Signature:
|
|
696
|
+
|
|
697
|
+
```ts
|
|
698
|
+
function startOfMillisecond<
|
|
699
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
700
|
+
>(dateTime: T): T
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
Returns the start of the current millisecond.
|
|
704
|
+
|
|
705
|
+
### `startOfMicrosecond`
|
|
706
|
+
|
|
707
|
+
Signature:
|
|
708
|
+
|
|
709
|
+
```ts
|
|
710
|
+
function startOfMicrosecond<
|
|
711
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
712
|
+
>(dateTime: T): T
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
Returns the start of the current microsecond.
|
|
716
|
+
|
|
717
|
+
## End Of Unit
|
|
718
|
+
|
|
719
|
+
End helpers return the last representable value before the next unit begins.
|
|
720
|
+
For date-time and zoned date-time values, this means one nanosecond before the
|
|
721
|
+
exclusive end. For date-only values, it means one day before the exclusive end.
|
|
722
|
+
For `Temporal.PlainYearMonth`, `endOfYear` returns December of the same year.
|
|
723
|
+
|
|
724
|
+
```ts
|
|
725
|
+
import { endOfDay, endOfMonth } from 'temporal-utils'
|
|
726
|
+
|
|
727
|
+
const dateTime = Temporal.PlainDateTime.from('2024-07-20T12:30:00')
|
|
728
|
+
|
|
729
|
+
endOfDay(dateTime).toString()
|
|
730
|
+
// '2024-07-20T23:59:59.999999999'
|
|
731
|
+
|
|
732
|
+
endOfMonth(dateTime).toString()
|
|
733
|
+
// '2024-07-31T23:59:59.999999999'
|
|
734
|
+
```
|
|
735
|
+
|
|
736
|
+
### `endOfYear`
|
|
737
|
+
|
|
738
|
+
Signature:
|
|
739
|
+
|
|
740
|
+
```ts
|
|
741
|
+
function endOfYear<
|
|
742
|
+
T extends
|
|
743
|
+
| Temporal.PlainYearMonth
|
|
744
|
+
| Temporal.PlainDate
|
|
745
|
+
| Temporal.PlainDateTime
|
|
746
|
+
| Temporal.ZonedDateTime,
|
|
747
|
+
>(date: T): T
|
|
748
|
+
```
|
|
749
|
+
|
|
750
|
+
Returns the last representable value in the current year.
|
|
751
|
+
|
|
752
|
+
### `endOfMonth`
|
|
753
|
+
|
|
754
|
+
Signature:
|
|
755
|
+
|
|
756
|
+
```ts
|
|
757
|
+
function endOfMonth<
|
|
758
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
759
|
+
>(date: T): T
|
|
760
|
+
```
|
|
761
|
+
|
|
762
|
+
Returns the last representable value in the current month.
|
|
763
|
+
|
|
764
|
+
### `endOfWeek`
|
|
765
|
+
|
|
766
|
+
Signature:
|
|
767
|
+
|
|
768
|
+
```ts
|
|
769
|
+
function endOfWeek<
|
|
770
|
+
T extends Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
771
|
+
>(date: T): T
|
|
772
|
+
```
|
|
773
|
+
|
|
774
|
+
Returns the last representable value in the current ISO week.
|
|
775
|
+
|
|
776
|
+
### `endOfDay`
|
|
777
|
+
|
|
778
|
+
Signature:
|
|
779
|
+
|
|
780
|
+
```ts
|
|
781
|
+
function endOfDay<T extends Temporal.PlainDateTime | Temporal.ZonedDateTime>(
|
|
782
|
+
date: T,
|
|
783
|
+
): T
|
|
784
|
+
```
|
|
785
|
+
|
|
786
|
+
Returns the last representable value in the current day.
|
|
787
|
+
|
|
788
|
+
### `endOfHour`
|
|
789
|
+
|
|
790
|
+
Signature:
|
|
791
|
+
|
|
792
|
+
```ts
|
|
793
|
+
function endOfHour<
|
|
794
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
795
|
+
>(date: T): T
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
Returns the last representable value in the current hour.
|
|
799
|
+
|
|
800
|
+
### `endOfMinute`
|
|
801
|
+
|
|
802
|
+
Signature:
|
|
803
|
+
|
|
804
|
+
```ts
|
|
805
|
+
function endOfMinute<
|
|
806
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
807
|
+
>(date: T): T
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
Returns the last representable value in the current minute.
|
|
811
|
+
|
|
812
|
+
### `endOfSecond`
|
|
813
|
+
|
|
814
|
+
Signature:
|
|
815
|
+
|
|
816
|
+
```ts
|
|
817
|
+
function endOfSecond<
|
|
818
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
819
|
+
>(date: T): T
|
|
820
|
+
```
|
|
821
|
+
|
|
822
|
+
Returns the last representable value in the current second.
|
|
823
|
+
|
|
824
|
+
### `endOfMillisecond`
|
|
825
|
+
|
|
826
|
+
Signature:
|
|
827
|
+
|
|
828
|
+
```ts
|
|
829
|
+
function endOfMillisecond<
|
|
830
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
831
|
+
>(date: T): T
|
|
832
|
+
```
|
|
833
|
+
|
|
834
|
+
Returns the last representable value in the current millisecond.
|
|
835
|
+
|
|
836
|
+
### `endOfMicrosecond`
|
|
837
|
+
|
|
838
|
+
Signature:
|
|
839
|
+
|
|
840
|
+
```ts
|
|
841
|
+
function endOfMicrosecond<
|
|
842
|
+
T extends Temporal.PlainTime | Temporal.PlainDateTime | Temporal.ZonedDateTime,
|
|
843
|
+
>(date: T): T
|
|
844
|
+
```
|
|
845
|
+
|
|
846
|
+
Returns the last representable value in the current microsecond.
|
|
847
|
+
|
|
848
|
+
## Shared Types
|
|
849
|
+
|
|
850
|
+
### `RoundingMode`
|
|
851
|
+
|
|
852
|
+
Type:
|
|
853
|
+
|
|
854
|
+
```ts
|
|
855
|
+
import type { RoundingMode } from 'temporal-utils'
|
|
856
|
+
```
|
|
857
|
+
|
|
858
|
+
`RoundingMode` is shared by the rounding and difference helpers.
|
|
859
|
+
|
|
860
|
+
### `RoundingMathOptions`
|
|
861
|
+
|
|
862
|
+
Type:
|
|
863
|
+
|
|
864
|
+
```ts
|
|
865
|
+
import type { RoundingMathOptions } from 'temporal-utils'
|
|
866
|
+
```
|
|
867
|
+
|
|
868
|
+
`RoundingMathOptions` contains the rounding fields used by single-unit math
|
|
869
|
+
helpers.
|
|
870
|
+
|
|
871
|
+
The unit is implied by the helper name. For example, `roundToWeek(date,
|
|
872
|
+
options)` forces `smallestUnit: 'week'` internally, and `diffMonths(date0,
|
|
873
|
+
date1, options)` forces month-based difference math internally.
|