biorubenfs
biorubenfs

Reputation: 725

Add days Temporal.Duration to Temporal.Instant() using javascript Temporal library

I'm working with Temporal javascript library to manage dates in a Node project and I have problem when I try to add an amount of days to a Instant type object, as following:

const now = Temporal.Now.instant()
const durationInDays = Temporal.Duration.from({days: 1})
const newDate = now.add(durationInDays)

The code compiles, but it launchs a runtime exception with the following message:

caused by: RangeError: Duration field days not supported by Temporal.Instant. Try Temporal.ZonedDateTime instead.

A quick solution that it works for me can be transform the days to hours:

const durationInHours = Temporal.Duration.from({hours: 1 * 24})
const newDate = now.add(durationInHours)

or maybe do that the error says, using a ZonedDateTime, transforming the Instant:

const newDate = now.toZonedDateTime('UTC').add(durationInDays).toInstant()

So my question is why I cannot add a Duration object with days to the Instant now

Upvotes: 0

Views: 304

Answers (0)

Related Questions