Calculate subscription trial metrics mathematically using Temporal.ZonedDateTime.
Use .since() to find elapsed active time and .until() for remaining time,
demonstrating accurate, balanced duration objects that handle month boundaries correctly.
Duration.compare)
-
// 1. Get current time point in the target time zone (or system default) const tz = selectedTimeZone || Temporal.Now.timeZoneId(); const now = Temporal.Now.zonedDateTimeISO(tz); // 2. Parse inputs const start = Temporal.PlainDate.from(startDateStr).toPlainDateTime(startTimeStr).toZonedDateTime(tz); const end = Temporal.PlainDate.from(endDateStr).toPlainDateTime(endTimeStr).toZonedDateTime(tz); // 3. Calculate with .since() and .until() const timeActive = now.since(start, { largestUnit: 'year', smallestUnit: 'minute' }); const timeRemaining = now.until(end, { largestUnit: 'year', smallestUnit: 'minute' }); // 4. Format with toLocaleString() (Intl.DurationFormat) and compare with relativeTo timeActive.toLocaleString('en-US', { style: 'narrow' }); Temporal.Duration.compare(timeActive, timeRemaining, { relativeTo: now });