Replies: 2 comments
-
Checking https://taro.codes/posts/2023-08-23-temporal-api/ by @lautarodragan, at least const now = Temporal.Now.zonedDateTimeISO()
const hours27 = Temporal.Duration.from({ hours: 27 })
hours27.toString()
> 'PT27H' // 27 hours, zero days
hours27.round({ smallestUnit: 'hour' }).toString()
> 'PT27H' // same thing, still 27 hours, zero days
hours27.round({ smallestUnit: 'hour', largestUnit: 'day' }).toString()
'P1DT3H' // 1 day, 3 hours
hours27.round({ smallestUnit: 'day', largestUnit: 'day' }).toString()
'P1D' // 1 day, which is what we want, but...
const hours23 = Temporal.Duration.from({ hours: 23 })
hours23.toString()
> 'PT23H'
hours23.round({ smallestUnit: 'day', largestUnit: 'day' }).toString()
> 'P1D' // 23 hours round up to 1 day, rather than down to 0 days
hours23.round({ smallestUnit: 'day', largestUnit: 'day', roundingMode: 'halfExpand' }).toString()
> 'P1D' // if we pass no roundingMode, it defaults to `halfExpand`. what we want here is `trunc`.
hours23.round({ smallestUnit: 'day', largestUnit: 'day', roundingMode: 'trunc' }).toString()
> 'PT0S' // boom! that's what we want Basically, the TC 39 cookbook for the Temporal API seems very handy, too. |
Beta Was this translation helpful? Give feedback.
0 replies
-
PR open #105. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Resources:
Beta Was this translation helpful? Give feedback.
All reactions