const duration = Temporal.Duration.from({ hours: 1, minutes: 30, seconds: 15 });
const roundedDuration = duration.round("minutes");
console.log(roundedDuration.toString()); // "PT1H30M"Temporal.Duration.prototype.round() / notebook
Temporal.Duration.prototype.round()
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const duration = Temporal.Duration.from({
days: 3,
hours: 1,
minutes: 41,
seconds: 5,
});
const roundedDuration = duration.round({ largestUnit: "hours" });
console.log(
`Time spent on this problem: ${roundedDuration.toLocaleString("en-US", { style: "digital" })}`,
);
// Time spent on this problem: 73:41:05const duration = Temporal.Duration.from({ days: 1, hours: 1, minutes: 30 });
const roundedDuration = duration.round({
largestUnit: "hours",
smallestUnit: "hours",
roundingMode: "floor",
});
console.log(roundedDuration.hours); // 25const duration = Temporal.Duration.from({ hours: 1, minutes: 17 });
const roundedDuration = duration.round({
smallestUnit: "minutes",
roundingIncrement: 15,
});
console.log(
`The queue will take approximately ${roundedDuration.toLocaleString("en-US")}`,
);
// The queue will take approximately 1 hr, 15 minconst duration = Temporal.Duration.from({ months: 1, days: 1, hours: 1 });
const roundedDuration = duration.round({
largestUnit: "days",
smallestUnit: "days",
relativeTo: Temporal.PlainDateTime.from("2022-01-01"),
});
console.log(roundedDuration); // "P32D"