Sub topic 15
Rounding Values and Durations
This chapter assumes you already know how to create Temporal values with from(), how Duration balancing works, and how ZonedDateTime relates to Instant. The examples use the global Temporal, either native or from the polyfill set up earlier.
Rounding Times and Date-Times
round() takes an options object. smallestUnit sets the precision. roundingIncrement snaps the value to multiples of that unit. roundingMode sets the direction, and its default is 'halfExpand', which rounds to the nearest increment with ties going away from zero.
const t = Temporal.PlainTime.from('14:37:50');
console.log(t.round('minute').toString()); // '14:38:00'
console.log(t.round({ smallestUnit: 'minute', roundingIncrement: 15 }).toString());
// '14:45:00'
console.log(t.round({ smallestUnit: 'minute', roundingIncrement: 15, roundingMode: 'floor' }).toString());
// '14:30:00'The increment has to divide the next larger unit evenly. For minutes you can use 15 or 20, but 7 throws a RangeError.
Rounding Durations
Duration.round() can also rebalance a duration with largestUnit. Years, months, and weeks have no fixed length, so rounding into them needs a relativeTo starting point.
const worked = Temporal.Duration.from({ hours: 1, minutes: 47 });
console.log(worked.round({ smallestUnit: 'minute', roundingIncrement: 15 }).toString());
// 'PT1H45M'
const span = Temporal.Duration.from({ days: 45 });
console.log(span.round({
largestUnit: 'month',
relativeTo: Temporal.PlainDate.from('2024-01-01'),
}).toString()); // 'P1M14D'Converting Between Temporal Types
There are two directions of conversion. Going from an exact or zoned value to a plain type throws information away, so it never needs extra input. Going from a plain type to an exact one needs whatever is missing: a time, a year, or a time zone.
const date = Temporal.PlainDate.from('2024-03-15');
const dateTime = date.toPlainDateTime(Temporal.PlainTime.from('09:30'));
console.log(dateTime.toString()); // '2024-03-15T09:30:00'
const zoned = dateTime.toZonedDateTime('Europe/Paris');
console.log(zoned.toString()); // '2024-03-15T09:30:00+01:00[Europe/Paris]'
console.log(zoned.toInstant().toString()); // '2024-03-15T08:30:00Z'
console.log(date.toPlainYearMonth().toString()); // '2024-03'
const holiday = Temporal.PlainMonthDay.from({ month: 12, day: 25 });
console.log(holiday.toPlainDate({ year: 2025 }).toString()); // '2025-12-25'Instant has no toPlainDate(). An exact moment falls on different dates in different places, so you convert it with toZonedDateTimeISO(timeZone) first.
Exercise
A timesheet entry runs from 08:52 to 17:08. Round the elapsed time to the nearest quarter hour and express it in hours and minutes.
Show answer
Temporal.PlainTime.from('08:52').until('17:08').round({ smallestUnit: 'minute', roundingIncrement: 15 }) gives 'PT8H15M'. The raw difference is 8 hours 16 minutes, and the nearest multiple of 15 minutes is 8 hours 15 minutes.
Exercise
You have an Instant and need the calendar date it falls on in Tokyo. Why does instant.toPlainDate() fail, and what is the correct call?
Show answer
Instant has no toPlainDate method, because one moment falls on different dates in different time zones. Use instant.toZonedDateTimeISO('Asia/Tokyo').toPlainDate().