javascript instance method
Temporal.Instant.prototype.subtract()
The subtract() method of Instant instances returns a new Temporal.Instant object representing this instant moved backward by a given duration (in a form convertible by Temporal.Duration.from()).
If you want to subtract two instants and get a duration, use since() or until() instead.
Syntax
subtract(duration)
Parameters
duration- : A string, an object, or a
Durationinstance representing a duration to subtract from this instant. It is converted to aTemporal.Durationobject using the same algorithm asTemporal.Duration.from().
- : A string, an object, or a
Return value
A new Instant object representing subtracting duration from this instant. If duration is positive, then the returned instant is earlier than this instant; if duration is negative, then the returned instant is later than this instant.
Exceptions
RangeError- : Thrown in one of the following cases:
durationis a calendar duration (it has a non-zeroyears,months, orweeks), or has a non-zerodays, because calendar durations are ambiguous without a calendar and time reference.- The result is not in the representable range, which is ±108 days, or about ±273,972.6 years, from the Unix epoch.
- : Thrown in one of the following cases:
Description
Subtracting a duration is equivalent to adding its negation, so all the same considerations apply.
Examples
Subtracting a Temporal.Duration
const instant = Temporal.Instant.fromEpochMilliseconds(1000);
const duration = Temporal.Duration.from("PT1S"); // One-second duration
const newInstant = instant.subtract(duration);
console.log(newInstant.epochMilliseconds); // 0
For more examples, see add().
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.